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?

jball
  • 24,791
  • 9
  • 70
  • 92
Exitos
  • 29,230
  • 38
  • 123
  • 178
  • 1
    Why are you putting utility methods in a `prototype`? – SLaks Jun 07 '10 at 16:06
  • please edit your code, so it's better readable. You can simply mark your javascript code and then click on the 'Code'-button the button with the ones and zeros – jigfox Jun 07 '10 at 16:06
  • 2
    possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – egrunin Jun 07 '10 at 16:06
  • It works for me: `/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test("a@b.com")` – SLaks Jun 07 '10 at 16:07
  • im using prototype so I can use it like an object? is this not a good idea? I made a utilities class – Exitos Jun 08 '10 at 13:56

1 Answers1

2

The regular expression that I'm using is

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

Try this one, should be a bit simpler :)

Ain Tohvri
  • 2,987
  • 6
  • 32
  • 51
  • There are TLDs that are more than four characters long. – SLaks Jun 07 '10 at 16:59
  • 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-method Sorry if these are too simple these questions still learning js...:-) – Exitos Jun 08 '10 at 13:55