SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help validating an email address

    Hi,

    I have a problem with validating email in javascript. I can do this in PHP but I have to use javascript for this and I am not a javascript expert.

    I am modifying an existing script and that has an email validation function. This works for most parts of validating an email but where it falls down is, when a user doesn't enter the .com (etc.) after the email ie: the current script lets them enter me@mysite where it should be me@mysite.com

    Can someone help me with adding a check to the function below, that will ensure a user enters a text, a . and then more text after the @ symbol.

    Thanks

    Here is how it looks at the moment:
    Code:
    function chkEmail(txt) {
    	if (!txt || txt.length < 1) {
    		OT_ErrAdd("You must enter an email address\n");
    		return false;
    	}
    	i = txt.indexOf('@');
    	if (i < 0) {
    		OT_ErrAdd("Your email address must contain an '@' symbol\n");
    		return false;
    	} else if (i == 0) {
    		OT_ErrAdd("Your email address cannot start with the '@' symbol\n");
    		return false;
    	} else if (i == txt.length-1) {
    		OT_ErrAdd("Your email address cannot end with the '@' symbol\n");
    		return false;
    	}
    	i = txt.indexOf('@', i+1);
    	if (i != -1) {
    		OT_ErrAdd("Your email address must contain only one '@' symbol\n");
    		return false;
    	}
    }
    Thanks in advance,

    Martin

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2003
    Location
    PA
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use reg-x:
    Code:
     function isValidEmail(emailAddress) {
    	 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    	 return re.test(emailAddress);
     }
    I'm no reg-x expert, but I do know this is the right way to go; I got this one online somewhere, months ago.

  3. #3
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do you know how I would combine that code into the existing function I have?

  4. #4
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can anyone help with this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •