3种方式:
1、 <!-- 验证表单的js -->
<script type="text/javascript">
function checkForm(form){
if(form.username.value == ""){
alert("请输入账号");
form.username.focus();
return false;
}
if(form.password.value == ""){
alert("请输入密码");
form.password.focus();
return false;
}
return true;
}
</script>
<form action="" method="post" id="jvForm">
账号:<input name="username" id="username" type="text"/><br/>
密码:<input name="password" id="password" type="text"/>
<input type="submit" value="登录" onclick="return checkForm(this.form)"/>
</form>
2、验证js函数一样,只是js函数添加在了form标签上
<form action="" method="post" id="jvForm" οnsubmit="return checkForm(this)">
账号:<input name="username" id="username" type="text"/><br/>
密码:<input name="password" id="password" type="text"/>
<input type="submit" value="登录"/>
</form>
3、<!-- 验证表单的js提交方式会不一样 -->
<script type="text/javascript">
function checkFormSubmit(form){
if(form.username.value == ""){
alert("请输入账号");
form.username.focus();
return false;
}
if(form.password.value == ""){
alert("请输入密码");
form.password.focus();
return false;
}
document.jvForm.submit();
}
</script>
<form name="jvForm" action="" method="post" id="jvForm">
账号:<input name="username" id="username" type="text"/><br/>
密码:<input name="password" id="password" type="text"/>
<input type="button" value="登录" οnclick="checkFormSubmit(this.form)"/>
</form>
4、<!-- 过滤非数字 -->
<script type="text/javascript">
function checkNum(form){
alert(form.username.value.match(/\D/));
/* D非数字 d数字 match即是否匹配非数字 没有非数字就是true */
return form.username.value.match(/\D/) == null;
}
</script>
function checkQQ(form){
if(form.username.value.match(/^\d{5,10}$/) == null){
return false;
}else{
return true;
}
}
<!-- js验证数字 中文 字符等 -->
<form action="" method="post">
<input type="text" name="username"/>
<input type="submit" οnclick="return checkNum(this.form)"/>
<input type="submit" οnclick="return checkQQ(this.form)"/>
</form>