欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js 电话验证

程序员文章站 2022-07-15 16:49:44
...

核心 js 代码:

function checkEmployName() {

	if($('#employ_name').val() == "") {
		$('#lab_name').html("*用户名不能为空");
		$('#employ_name').focus();
	} else {
		$('#lab_name').html("*验证通过 √");
	}

}

function checkEmployPhone() {
	var isMobile = /^(?:13\d|15\d|18\d|17\d)\d{5}(\d{3}|\*{3})$/; //手机号码验证规则
	//var isPhone = /^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/; //座机验证规则例如:13415764179或0321-4816048"

	var phone = $('#employ_phone').val(); //获得用户填写的号码值 赋值给变量dianhua

	if($('#employ_phone').val() == "") {
		$('#lab_phone').html("*手机号码不能为空");
		$('#employ_name').focus();
	}else if(!isMobile.test(phone) ||phone.length !=11) { //如果用户输入的值不同时满足手机号和座机号的正则
		alert(phone.length)
		$('#lab_phone').html("*请正确填写手机号码");
		$("#employ_phone").focus(); //输入框获得光标
	//	return false; //返回一个错误,不向下执行
	} else {
		$('#lab_phone').html("*验证通过 √");
		 $("#btn_ok").removeAttr("disabled");
	}

}

前端jsp页面:

	<div class="row">
		<div class="col-12 grid-margin stretch-card">
			<div class="card">
				<div class="card-body">

					<p class="card-description">请 <code> 添加</code> 职工基本信息</p>

					<form class="form-inline">
						<span>

						<div class="input-group mb-2 mr-sm-2">
							<div class="input-group-prepend">
								<div class="input-group-text">姓名</div>
							</div>
							<input id="employ_name" name="employ_name" type="text" class="form-control"
                                   placeholder="请输入职员姓名" onblur="checkEmployName()">
						</div>
								<label id="lab_name" style="color: red;font-size: small;"
                                       for="employ_name"></label>
						</span>
						<span>

						<div class="input-group mb-2 mr-sm-2">
							<div class="input-group-prepend">
								<div class="input-group-text">电话</div>
							</div>
							<input id="employ_phone" name="employ_phone" type="text" class="form-control"
                                   placeholder="请输入职员电话" onblur="checkEmployPhone()">
						</div>
							<label id="lab_phone" for="employ_phone"></label>
						</span>
						<span>
						<button id="btn_ok" type="submit" class="btn btn-gradient-success mb-2">清除
						</button>
						</span><span>
						<label style="padding-left: 10px;"></label></span><span>
						<button id="btn_clear" type="submit" class="btn btn-gradient-primary mb-2">添加</button>
                    </span>
					</form>
				</div>
			</div>
		</div>
	</div>

 

相关标签: Validate