jsp怎么在提交表单前验证javascript实例

jsp怎么在提交表单前验证javascript实例,在提交表单时,我们常常需要验证核实表单内容,若都不为空时,方能提交;若有文本框为空则不提交,并获取鼠标焦点到文本框上 ,所以我们可以利用onsubmit的方法来做,请看下面例子:

<html>

<head><title></title>

<script>

function yan(){

if(frm.stuname.value==”"){

alert(“姓名不得为空!”);//弹出提示框

document.frm.stuname.focus();  //获取鼠标焦点

return(false);//返回一个值

}

else if(frm.stuPWD.value==”"){

alert(“密码不得为空!”);//弹出提示框

document.frm.stuPWD.focus();  //获取鼠标焦点

return(false);//返回一个值

}

}

</script>

</head>

<body>

<form onsubmit=”return yan()”>

<table>

<tr>

<td>用户名:<input type=”text” id=”stuname”></td>

</tr>

<tr>

<td>密码:<input type=”password” id=”stuPWD”></td>

</tr>

<tr>

<td><input type=”submit” value=”提交”></td>

</tr>

<table>

</form>

</body>

</html>

这样就可以了,按提交时就会触发方法,注意的是,onsubmit=”return yan()”,里面一定要加上return,否则没有返回值的话,方法会失效 本文链接地址: jsp怎么在提交表单前验证javascript实例