我想实现javascript 验证失败的话,不能提交表单,这个该如何实现呢,还请各位大侠赐教!

functioncheckform(form){vars_n=operation_form.stu_num.value;varc_n=operation_form.cel... function checkform(form)
{var s_n=operation_form.stu_num.value;
var c_n=operation_form.cellphone.value;
if (operation_form.stu_num.value=="" )
{
alert("提交失败,账号不能为空");
myform.user.focus();
return false;
}

if (operation_form.cellphone.value=="")
{
alert("提交失败,必须要填写用户的联系方式");
operation_form.cellphone.focus();
return false;
}

if (operation_form.stu_num.value=="" || operation_form.stu_num.value.length>11)
{
alert('提交失败,账号不能为空且账号长度小于11位');
operation_form.stu_num.focus();
return false;
}
if(isNaN(s_n))
{
alert('提交失败,账号须为数字,请检查!');
operation_form.stu_num.focus();
return false;
}

if (operation_form.cellphone.value=="" || operation_form.cellphone.value.length>11)
{
alert('提交失败,联系方式不能为空且账号长度小于11位');
operation_form.cellphone.focus();
return false;
}
if(isNaN(c_n))
{
alert('提交失败,联系方式须为数字,请检查!');
operation_form.cellphone.focus();
return false;
}
else
{ return true;}

}
</SCRIPT>

<form action='add.php' method='post' name='operation_form' onsubmit="return checkform(this.form);">

if($_POST[submit])
{
var_dump($_POST[submit]);
echo "<br/>";
$pas=' ';
$pass_sec=md5($pass);
if($pass_sec!=md5($pas))
{
echo "<script language=\"javascript\">alert('密码错误,提交失败!');history.go(-1)</script>";
}
else
{
echo "<script language=\"javascript\">alert('密码正确');</script>";
该代码执行后,假如我输入密码正确后,即使用户名为空,却可以能执行index.php,我想实现假如我没有填写用户名,就不能提交表单执行PHP
展开
 我来答
匿名用户
推荐于2016-06-01
展开全部
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
}

td {
border: 1px solid black;
}
</style>
<script type="text/javascript">
function checkform (form)
    {
var reg = /^\s*|\s*$/g;
var stuname = form.stu_num.value.replace(reg, '');
var cellphone = form.cellphone.value.replace(reg, '');
    if (stuname === "")
    {
    alert ("提交失败,账号不能为空!");
    form.stu_num.focus ();
    form.stu_num.value = '';
    return false;
    }
    else if (!/^\d+$/.test(stuname))
    {
    alert ('提交失败,账号须为数字,请检查!');
    form.stu_num.focus ();
    form.stu_num.select();
    return false;
    }
    else if (/.{11,}/.test(stuname))
    {
    alert ('提交失败,账号长度必须小于11位!');
    form.stu_num.focus ();
    form.stu_num.select();
    return false;
    }
    else if (cellphone === "")
    {
    alert ("提交失败,必须要填写用户的联系方式!");
    form.cellphone.focus ();
    form.cellphone.value = '';
    return false;
    }  
    else if (!/^\d+$/.test(cellphone))
    {
    alert ('提交失败,联系方式须为数字,请检查!');
    form.cellphone.focus ();
    form.cellphone.select();
    return false;
    }
    else if (/.{11,}/.test(cellphone))
    {
    alert ('提交失败,联系方式长度必须小于11位!');
    form.cellphone.focus ();
    form.cellphone.select();
    return false;
    }
    else
    {
    return true;
    }
    }
</script>
</head>
<body>
<form action="add.php" method="post" name="operation_form" onsubmit="return checkform(this);">
<table>
<tr>
<td>账号:</td>
<td><input type="text" name="stu_num" /></td>
</tr>
<tr>
<td>联系方式:</td>
<td><input type="text" name="cellphone" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center;"><input type="submit" value="submit" /> <input type="reset" value="reset" /></td>
</tr>
</table>
</form>
</body>
</html>
追问
谢谢,我上面的代码,哪里出了问题,导致重复执行
追答
  1. onsubmit 方法参数不当

  2. if else if 使用不当

  3. 验证不当

  4. 等等

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式