jviotti
jviotti

Reputation: 18909

What is wrong with this email RegExp?

I'm trying to implement this (the one at the bottom of the page) RegExp to validate email addresses with jquery validation plugin.

This is my code:

$.validator.addMethod("email_address", function(value, element, param) {
  var email_regexp = new RegExp("[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:a-z0-9?.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b", "g");
  var result = value.match(email_regexp);
  return result ? result.length >= param : false;
}, "Invalid email address");

No JS errors are shown, still it doesn't validate anything! Been playing with it for like an hour and can't get this working!

Is there something wrong?

EDIT: I tried also with // delimiters:

$.validator.addMethod("email_address", function(value, element, param) {
  var result = value.match(/[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:a-z0-9?.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/g);
  return result ? result.length >= param : false;
}, "Invalid email address");

Upvotes: 0

Views: 159

Answers (2)

Sparky
Sparky

Reputation: 98718

Why are you writing a custom regex function for the jQuery Validate plugin when it already has an email rule built-in?

http://docs.jquery.com/Plugins/Validation/Methods/email

jQuery:

$(document).ready(function() {

    $('#myform').validate({
        rules: {
            field: {
                required: true,
                email: true
            }
        }
    });

});

HTML:

<form id="myform">  
     <input type="text" name="field" />  <br/>  
     <input type="submit" />
</form> 

Working Demo:

http://jsfiddle.net/sRwHc/


The default regex function used within the .validate() plugin, FYI:

email: function(value, element) {
    // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
    return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
}

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385108

~-]+(?:\.[a-z0-9!#$
//     ^^

That \. will need escaping again for the Javascript string:

~-]+(?:\\.[a-z0-9!#$
//     ^^^

Or, preferably, use // delimeters rather than constructing a RegExp object from a string.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp

Upvotes: 2

Related Questions