当前位置: 首页 > 工具软件 > Validate.js > 使用案例 >

jQuery Validate.js验证手机号码。

艾嘉石
2023-12-01

html:

<div class="edit_phone1 tis_edit">
<form  id="cell" class="form-horizontal" method="get" action="">
	<div class="select_div bkbj">
		<span style="width:86px;">原手机号码:</span>
		<input type="text" disabled="disabled" id="old_phone2">
	</div>
	<div class="select_div new-phone" style="margin-bottom: 0;">
		<span  style="width:86px;">新手机号码:</span>
		<input type="text" id="new_phone" name="new_phone" autofocus="autofocus">
	</div>
	<div style="color:red;text-align: center;">
		<p style="margin-right: 54px;">备注:新手机号码将替代原手机号码</p>
		<p style="margin-left: 17px;">该教师的登录账号将为新手机号码</p>
	</div>
	</form>
</div>

js:

$(function(){
	$("#cell").validate({
	    rules: {
	    	new_phone : {  
	            required : true,  
	            minlength : 11, 
	            isMobile : true  
	        		}, 
	          },
	   messages: {
		   new_phone : {  
		       required : "请输入手机号",  
		       minlength : "不能小于11个字符",  
		       isMobile : "请正确填写手机号码"  
		  		 	}
  				 },
		});
})

自定义验证手机规则(一定要写):

//手机号码验证  
jQuery.validator.addMethod("isMobile", function(value, element) {  
 var length = value.length;  
 var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;  
 return this.optional(element) || (length == 11 && mobile.test(value));  
}, "请正确填写手机号码");  

 类似资料: