直接上图
<script>
function checkForm() {
/*alert("aaa00");*/
var c_sname = document.getElementById("s_name").value;
var regExp = /^[\u4e00-\u9fa5]+$/ ;
if(!c_sname.match(regExp)){
alert("请输入正确的姓名");
return false;
}
var c_studentid = document.getElementById("s_studentid").value;
var regExp2 = /^[0-9]+$/;
if(!c_studentid.match(regExp2)){
alert("请输入正确的学号");
return false;
}
var c_classid = document.getElementById("s_classid").value;
var regExp2 = /^[0-9]+$/;
if(!c_classid.match(regExp2)){
alert("请输入正确的班级编号");
return false;
}
var c_classname = document.getElementById("s_classname").value;
var regExp = /^[\u4e00-\u9fa5]+$/ ;
if(!c_classname.match(regExp)){
alert("请输入正确的班级名");
return false;
}
}
</script>
另外一种写法
(我写的时候老是没有反应)
function checkForm() {
/*alert("aaa00");*/
var c_sname = document.getElementById("s_name").value;
if(!/^[\u4e00-\u9fa5]+$/.test(c_sname )){
alert("请输入正确的姓名");
return false;
}
<form class="navbar-form navbar-left" role="search" onsubmit="return checkForm()">
<div class="form-group" style="position: relative;left: -30px;">
<input required:!important type="text" class="form-control" placeholder="请输入姓名" name="s_name" id="s_name">
<input type="text" class="form-control" placeholder="请输入学号" name="s_studentid" id="s_studentid">
<input type="text" class="form-control" placeholder="请输入班级编号" name="s_classid" id="s_classid">
<input type="text" class="form-control" placeholder="请输入班级名" name="s_classname" id="s_classname">
<input class="layui-input" type="hidden" name="pageIndex" value="1">
<input class="layui-input" type="hidden" name="pageSize" value="4">
</div>
<button type="submit" class="btn btn-default btn-lg">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> Star
</button>
</form>
有个问题就是
我想进行简单的数字和文字校验,但不想对空(未输入,它也弹出来)进行处理,有哪位大佬知道的话指点一下,谢谢。
问题解决如下
用嵌套if
先判断是否为空,如果为空则不进行校验,如果不为空进行校验
Onsubmit监听改成OnChange监听
<script>
function checkForm() {
var c_sname = document.getElementById("s_name").value;
var regExp = /^[\u4e00-\u9fa5]+$/ ;
if(!c_sname==""){
if(!c_sname.match(regExp)){
alert("请输入正确的姓名");
return false;
}
}
var c_studentid = document.getElementById("s_studentid").value;
var regExp2 = /^[0-9]+$/;
if(!c_studentid==""){
if(!c_studentid.match(regExp2)){
alert("请输入正确的学号");
return false;
}
}
var c_classid = document.getElementById("s_classid").value;
var regExp2 = /^[0-9]+$/;
if(!c_classid==""){
if(!c_classid.match(regExp2)){
alert("请输入正确的班级编号");
return false;
}
}
var c_classname = document.getElementById("s_classname").value;
var regExp = /^[\u4e00-\u9fa5]+$/ ;
if(!c_classname==""){
if(!c_classname.match(regExp)){
alert("请输入正确的班级名");
return false;
}
}
}
</script>