Reputation: 23
I have a form I'm trying to do validation on. Requirements state that underscore and hyphen are allowed in the email username, but NO special characters (including hyphen and underscore) are allowed as the leading and/or trailing character of the email username.
Would this be done using regex in the form validation? Or would it be done in a separate jquery function? I'm trying to fire an error in the case that a special character is used as the first or last character of the email username.
Here's my validation code for the email field:
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);
},
And here's the code for the registration form validation:
if (form == 'registration') {
var validation_rules = {
rules: {
password_confirm: {
equalTo: "#password"
},
email: {
},
email_confirm: {
equalTo: "#email"
},
dd_reg_month: "required"
},
groups: {
birthday: "dd_reg_month dd_reg_year"
},
errorPlacement: function(error, element) {
if (element.attr("name") == "dd_reg_month" || element.attr("name") == "dd_reg_year") $("#reg_birthday").append(error);
else error.insertAfter(element);
},
validClass: "success",
success: "success",
focusInvalid: false,
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
var ipAddress =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if (errors) {
var message = errors == 1 ? 'Please correct the error below' : 'Please correct the ' + errors + ' errors below.';
$("div.error_info").html(message);
$("div.error_info").show();
} else {
$("div.error_info").hide();
$('form input.success').css('border-color', 'green');
}
},
ignore: ".ignore"
};
}
Any help, even a clue as to how to deal with leading and trailing characters, would be totally awesome.
Upvotes: 0
Views: 1397
Reputation: 23
thanks for the contribution guys! I found some regex that did the trick - this site is really helpful: http://regexlib.com/DisplayPatterns.aspx
The pattern I used was this, posted by Rob Eberhardt:
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
Figured I'd share in case anyone comes across the same problem. Check out the regex library if you need to do other things with email validation, it's got a lot of great patterns. I'm new to regex and hadn't heard of it yet until today. :-)
Thanks again!
Upvotes: 1
Reputation:
I don't know jquery, but if the regexes are .NET maybe just inserting a couple of lookahead and lookbehind assertions might solve the problem.
This is expanded. Just look where they are added and insert them into your regex.
If it works great, if not, was worth the try.
/
^
(
(?![_-])
(
(
[a-z]
| \d
| [\-\_]
| [\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]
)+
(
\.
(
[a-z]
| \d
| [\-\_]
| [\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]
)+
)*
(?<![_-])
)
|
(
(\x22) "
(?![\x01-\x20\x7f]*[_-])
(
(
(
(\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)+
)?
(?<![_-][\x01-\x20\x7f]*)
(\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
Upvotes: 0
Reputation: 11383
If the special requirements should trigger the same error as a regular invalid email you can put it in the validation function.
It could probably be included in the email validation regex but I'm not going to dive into that huge thing to change it. My code below will tack on an extra regex, after the email is validated, to test for your special case.
email: function(value, element) {
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
var valid = 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);
return valid && /^[a-z].*[a-z]@.+/i.test(value);
},
Upvotes: 0