form表单的简单验证onsubmit

form表单的代码

<form name="myform" action="" method="post">
<input type="text" name="search" id="scontent" placeholder="请输入" value=""/>
<select id="stype" name="stype">
<option  value="name">姓名</option>
<option  value="id_card">身份证号</option>
<option  value="mobile">电话</option>
</select>
<input id="stj" type="submit" onclick="return submitTest()" value="查询" />
</form>

 

js代码

<script>
function submitTest(){
var scontent = document.myform.search.value;
var stype = document.myform.stype.value;
var l = scontent.length;
if(scontent==""){
alert("输入框的内容不能为空");
return false;
}
if(stype == 'id_card'){
if(l!=15||l!=18){
alert("身份证号必须是15或18位");
return false;
}
}else if(stype == 'name'){
if(l>32){
alert("名字的长度不能大于32位");
return false;
}
}else{
if(15<l||l<7){
alert("电话必须是7到15位");
return false;
}
}
}
</script>

 

这是用提交按钮触发的onclick事件,还可以用form表单中的onsubmit来调用

<form name="myform" action="" onsubmit="return submitTest()"  method="post">

posted @ 2015-08-21 19:08  到站了  阅读(608)  评论(0编辑  收藏  举报