-1

I have used jQuery validate to validate email addresses but while testing it accepts email addresses like ak@cmmmm.c or ak@gh.com.com which should not be accepted. So can I edit jquery.validate.js and insert my own regex. If any better solution please help me.

Thanks, any help from you is valuable for me.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Akki
  • 1,718
  • 2
  • 29
  • 53

2 Answers2

1

Editing the source code of a plugin is never a good idea - what happens when you want to upgrade to a newer version?

You can however write you own jQuery vlaidate plugins, a overview of how to do this is available here.

That being said, the built in email regex is about as robust as you're going to get. There isn't a great deal you can do to prevent cases like you have pointed out without breaking the email validation for other cases.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

For sure you can:

As far as I understood this is your jquery.validate.js plugin. http://code.google.com/p/jqueryjs/source/browse/trunk/plugins/validate/jquery.validate.js?r=6243

In the line 964 you have your function that validates email:

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);
                },

Just substitute that huge line, with the expression you want to and this will be ok. Here is an example of regex for email: Validate email address in JavaScript?

I do not want to through to you my validator, because I have no idea what exactly do you need as a right one.

But keep in mind there is no way that you will be sure that the email the person entered is a correct one. justforfun@gmail.com will pass all validators, but how can you be sure that it is a valid user's email.

If a person will be truly interested in providing a right email - he will check a dozen of times (never seen nice validators on any embassy web-site)

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753