前端简单校验

直接上图

<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值