我有一个我正在尝试对其进行验证的表单。要求规定电子邮件用户名中允许使用下划线和连字符,但不允许使用特殊字符(包括连字符和下划线)作为电子邮件用户名的前导和/或尾随字符。
这会在表单验证中使用正则表达式来完成吗?还是会在单独的 jquery 函数中完成?如果将特殊字符用作电子邮件用户名的第一个或最后一个字符,我会尝试触发错误。
这是我的电子邮件字段验证码:
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);
},
这是注册表单验证的代码:
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"
};
}
任何帮助,甚至是关于如何处理前导和尾随字符的线索,都会非常棒。