为了避免表单数据被机器批量注入,因此需要对数据进行验证是否符合标准。本次验证主要是验证手机号码是否11位,号码号段格式是否正确,以及电子邮件是否按照格式来写的。

1.将如下代码放在有留言的模版的head里面。

<script language="javascript">
function CheckForm()
{
var tel = document.zhimatong.tel.value;
if (tel.length == 0) {
alert("请输入手机号码!");
document.zhimatong.tel.focus();
return false;
}
if (tel.length !== 11) {
alert("请输入有效的手机号码!");
document.zhimatong.tel.focus();
return false;
}
if(!tel.match(/^1[3|4|5|8][0-9]\d{4,8}$/)){
alert("请输入有效的手机号码!");
document.zhimatong.tel.focus();
return false;
}
var email = document.zhimatong.mail.value;
if (email.length == 0) {
alert("请输入电子邮箱!");
document.zhimatong.mail.focus();
return false;
}
if(!email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
alert("请输入正确的电子邮箱!");
document.zhimatong.mail.focus();
return false;
}
return true;
}
</script>

2.修改form

<form action="{pboot:msgaction}" method="post" name="zhimatong" onsubmit="return CheckForm();">

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注