Jquery Validation驗證固話+手機號碼
阿新 • • 發佈:2018-11-24
先引入jQuery+jQuery Validation.js
<div class="edit_phone1 tis_edit"> <form id="cell" class="form-horizontal" method="get" action=""> <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> </form> </div>
$(function(){ $("#cell").validate({ rules: { new_phone : { required : true, isMobile : true }, }, messages: { new_phone : { required : "請輸入手機號/固話", isMobile : "請輸入手機號/固話" } }, }); //手機號碼驗證 jQuery.validator.addMethod("isMobile", function(value,element) { var length = value.length; var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/; var tel = /^\d{3,4}-?\d{7,9}$/; if(/-/.test((value))){ return this.optional(element) || (tel.test(value)) }else { return this.optional(element) ||(length==11 &&mobile.test(value)) } }, "請正確填寫您的聯絡電話"); })