views:

56

answers:

1

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.

+3  A: 

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.

SLaks
Elegant (as you were last time). It's not Jquery based, but it's so simple and elegant I can write it inline.Thank you =)
Josh
@Josh: You're welcome! jQuery is a DOM library and has nothing to do with string manipulation; it wouldn't help here. (Except for a change event)
SLaks