jQuery Validate.js验证手机号码。

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));  
}, "请正确填写手机号码");  

猜你喜欢

转载自blog.csdn.net/Keith003/article/details/83412470