关于PHP表单验证程序的疑问

来源:百度知道 编辑:UC知道 时间:2024/05/16 10:51:06
我用PHP做的一个会员注册页面,当点按钮提交时,首先用js进行验证,大概代码是:
<input type="submit" name="Submit" value="提交" onclick="checkform()"/>
而checkform()中的部分代码为:
checkform(){
var fieldvalue=document.form1.username.value;
if(fieldvalue ==''){
alert('账号不能为空');
document.form1.username.focus();
return false;
}
else if (fieldvalue.length<4 || fieldvalue.length>10){
alert('账号的长度必须是4到10个字符!');
document.form1.username.focus();
return false;
}
}
但问题来了,如果我输入一个不合规范的信息注册(如三个字符的用户名),点提交后,JS会弹出警告框,但接下来就没有返回,而是把注册信息写入了数据库。
请问我代码哪出了问题,该怎么改?
谢谢,果然加了个return就可以了。但能否讲下原因?我看别的要调用onclick的事件引号里直接写函数就可以了啊
还有,改动以后,不只是点击提交按钮它会对表单进行格式检查,切换到别的页面它也要弹出alert对话框。

<input type="submit" name="Submit" value="提交" onclick="return checkform()"/>

checkform = function (){
........
.......
通过验证,则:return true;
}

不加return ,则是onclick=false;
不能使表单无效。

常规的表达验证

<input type="button" name="Submit" value="提交" onclick="checkform()"/>

checkform(){
var fieldvalue=document.form1.username.value;
if(fieldvalue ==''){
alert('账号不能为空');
document.form1.username.focus();
return false;
}
else if (fieldvalue.length<4 || fieldvalue.length>10){
alert('账号的长度必须是4到10个字符!');
document.form1.username.focus();
return false;
}

document.form1.submit();

}