2

Possible Duplicate:
Validate email address in Javascript?

I am trying to use this javascript to check for a valid email, but what I don't need it to do is check to see if the field is blank in the form in case someone doesn't have an email address (don't ask).

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}

function validate_form(thisform)
{
with (thisform)
{
  if (validate_email(email,"Not a valid e-mail address.")==false)
  {email.focus();return false;}
}
}

I tried to adjust the apos<1 to less than 1 or nothing at all and that didn't seem to work.

Community
  • 1
  • 1
ultraloveninja
  • 1,969
  • 5
  • 27
  • 56
  • http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address –  Jul 24 '12 at 15:07
  • 1
    Don't try to validate email adresses syntactically. Read http://www.regular-expressions.info/email.html – Bergi Jul 24 '12 at 15:07

2 Answers2

2

Just check whether it is empty, and otherwise apply your email regex/validation function on it.

Also, you should a) not use with and b) not alert from the test function.

function test_email(address) {
    var atpos = address.indexOf("@"),
        dotpos = address.lastIndexOf(".");
    if (atpos < 1) // "@" at position 0 or not found (-1)
        return false;
    if (dotpos-atpos < 2) // last "." before position 2 or not found (-1)
        return false;
    if (atpos > dotpos) // last "." before the "@"
        return false;
    return true;
}

function validate_form(thisform) {
    var input = thisform.email;
    if (input.value) // != ""
        if (!test_email(input.value)) {
            alert("Not a valid e-mail address.");
            email.focus();
            return false;
        }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok. First, THANKS this is extremeley helpful. – ultraloveninja Jul 24 '12 at 15:20
  • Your indexOf check should have worked. Why do you think it doesn't? – Bergi Jul 24 '12 at 15:21
  • I see you used (address) rather than (field,alerttxt). Is this to not use the with and not have it set to check the field? Just want to make sure I am understanding this correctly. Thanks again! – ultraloveninja Jul 24 '12 at 15:25
  • [Don't use `with` at all](http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/). In my code the `address` variable is just a string, the function called with `input.value`. A function for checking a string should not expect an input object as an argument. – Bergi Jul 24 '12 at 15:32
0

A regular expression is probably the best approach, and a previous question has a great regular expression to use, though there are more complex and complete ones available. Validate email address in JavaScript?

Taking that regular expression and fitting into your code, you'd have a function looking something like this

function validate_email(field,alerttxt) {
    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,}))$/;
    if (re.test(field)) {
        return true;
    } else {
        alert(alerttxt);
        return false;
    }
}
Community
  • 1
  • 1
  • Is there any reason why something like indexOf isn't really useful? Is it because there are ways to "trick" it by adding spaces and such? – ultraloveninja Jul 24 '12 at 15:33
  • It's because there is more to a valid email address than just a '@' and a '.' in a specific position. This regular expression tries to take them into account. If you are feeling brave have a look at RFC822 for a specification - http://www.w3.org/Protocols/rfc822/ – Robert Price Jul 24 '12 at 16:17