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.
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:
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!