表单验证的表达的问题?

来源:百度知道 编辑:UC知道 时间:2024/05/24 11:01:17
一个form表单,onsubmit="return check()"(用js编写的function check()
{
if(document.form.username.value.length<6)
{
alert("不能少于6个字符!");
history.back();

return false;
}
return true;
})
若提交submit,则先验证,在到添加到数据库的具体语言(此处的代码是用if request.ServerVariables("request_method") = "POST" then控制的)。但是实际中有个问题出现了,若不填写username,先报错,点击确定后,仍出错(数据库设置的username不能为空)。这样好像,这个check没起作用,怎么解决?(就用这种方法)

正好最近我也遇到这个问题,这是由于控制不严造成的,你改一下,写成如下试试:

<html>
<head>
<script>
function check()
{
if(document.getElementById('username').value.length<=6)
{
alert('字符太少!');
document.getElementById('username').focus();
return false;
}
}
</script>
</head>
<body>
<form onSubmit="return check();" method="post" action="abc.html">
<input type="text" id="username" value="" />
<input type="submit" />
</form>
</body>
</html>

然后在输入框username下,增加id='username',至于js的书写,没有必要那么多return true;上面的就可以了.你去试下,我测试是没有问题的.