Joshua Enfield
Joshua Enfield

Reputation: 18308

Jquery - Validating Email

I do NOT want to use the Jquery attachment to the field. I just want a function I can call and pass in a value and get back a result on if it is a valid email. JQuery documentation seems to indicate this is possible but the documentation falls short on examples outside of attaching it to the field.

I realize I can use third-party functions, but it would be nice to know how to do this with Jquery since I do this alot in projects.

Upvotes: 0

Views: 125

Answers (1)

SLaks
SLaks

Reputation: 888185

You should validate the email address properly on the server, and only use a simple regex on the client.

For example:

function isValidEmail(value) { 
    return /.+@.+\..+/.test(value);
}

You can replace the regex with something more sophisticated, but don't get carried away. In particular, make sure you allow [email protected]. (Which many sites sadly and incorrectly reject)

On the server side, you should use a proper, non-regex-based validator.

Upvotes: 3

Related Questions