我设法调整了一个 jquery 表单验证示例以与我的网站一起使用,但我不知道如何确保它只是在电话号码字段中输入的数字和在电子邮件字段中输入的电子邮件地址。
请问有人可以帮忙吗?
还想知道有什么可以改进的吗?
<script language="javascript" type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
<script>
$(function() {
$( "#quickform" ).validate({
rules: {
name: {
required: true
},
phone: {
required: true,
phonevalidation: true
},
email: {
required: true,
emailvalidation: true
},
enquiry: {
required: true
}
},
messages: {
name: {
required: "Please enter your name."
},
phone: {
required: "Please enter your phone number."
},
email: {
required: "Please enter your email address."
},
enquiry: {
required: "Please enter your enquiry."
}
},
});
$.validator.addMethod("phonevalidation",
function(value, element) {
return /^[A-Za-z\d=#$%@_ -]+$/.test(value);
},
"Please enter a valid phone number."
);
$.validator.addMethod("emailvalidation",
function(value, element) {
return /^[A-Za-z\d=#$%@_ -]+$/.test(value);
},
"Please enter a valid email address."
);
});
</script>
编辑:
示例中已经有验证方法,但不接受特殊字符。我只需要了解如何将 /^[A-Za-z\d=#$%@_ -]+$/ 更改为电子邮件和号码验证。