views:

46

answers:

1

Can't believe how difficult this seems to be all I want to is to validate a user inout using javascript to make sure that it is an email address. But can't get it to work:

I am using:

//validates a regulaer expression
Utilities2.prototype.validateEmail = function(stringToValidateArg)
{

    alert('about to check regexp');

    var regExpPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

    alert(regExpPattern.test(stringToValidateArg));

}

But this always returns false, any ideas why is it because of the regular expression?

+2  A: 

The regular expression that I'm using is

/([\w-\.\+]+\@[\w-]+\.+[\w]{2,4})/gi

Try this one, should be a bit simpler :)

Ain
There are TLDs that are more than four characters long.
SLaks
Hi, okay thanks I tested this and yeah it works however now Im having a problem passing through that regex from an html onclick event:Ive posted up @http://stackoverflow.com/questions/2997879/having-problems-passing-a-regular-expression-through-to-a-methodSorry if these are too simple these questions still learning js...:-)