9

I have a field for users to write their e-mail address in. It is optional, so I need to first check if something was entered in the field, if nothing was entered then do nothing, but if something was entered than check if it is an e-mail.

Right now I'm at this point

var email = $("input#sc_email").val();  
if (email == "") {                
    // If e-mail field is empty do nothing
} else {                          
     // If something was entered
    // [CODE HERE] Check if valid e-mail was entered, if not show error message
    $("label#email_error").show(); //error message
    $("input#sc_email").focus();   //focus on email field
    return false;  
} 

I'm not even sure if that is right, but I think it is. Right now I need help with gaps in code, I marked them as [CODE HERE]. Could anyone suggest a solution please.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ilja
  • 44,142
  • 92
  • 275
  • 498

3 Answers3

23

You could use the jQuery validate plugin for this, but if you only want to check the validity of an email address in one field, that is a little bit of overkill.

Instead, the regular expression in the function below will make sure the format of the email address is correct, should a value have been entered.

var email = $("input#sc_email").val();  

if (email !== "") {  // If something was entered
    if (!isValidEmailAddress(email)) {
        $("label#email_error").show(); //error message
        $("input#sc_email").focus();   //focus on email field
        return false;  
    }
} 

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
};
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • It uses a regular expression (http://en.wikipedia.org/wiki/Regular_expression) to check the format of the string. It returns a boolean stating whether the string matched the RFC standard format for an email address (http://www.ietf.org/rfc/rfc5322.txt). – Rory McCrossan Dec 06 '11 at 10:39
  • yep tested it, works perfectly thank you ))) just need to wait to accept the answer ))) – Ilja Dec 06 '11 at 10:43
2

You could use regular expressions to validate the email address in JavaScript.

function validateEmail(email) { 
    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(email);
}

This will, however, only validate that it is a valid email address formatted string. flibble@flobble.com would be valid, even if no-one actually has that email address.

Have a look at this question.

Community
  • 1
  • 1
Connell
  • 13,925
  • 11
  • 59
  • 92
1

I would recommend validation both at client side and at server side, you could simply use this code:

alert(/\S+@\S+\.\S+/.test('asdasd@asdasd@asdd.com'));
alert(/\S+@\S+\.\S+/.test('asdasd234@asdd.com'));

but importantly, use server side validation and methodology, like sending click-link-mail, to verify your client email address or mailing random number etc.

Micho
  • 3,929
  • 13
  • 37
  • 40
Asghar
  • 11
  • 2