<div class="container">
<h1>validate.js 台灣手機號碼認證範例</h1>
<form id="myform">
<div class="form-group">
<label for="exampleInputEmail1">手機號碼</label>
<input type="text" class="form-control" id="phone" placeholder="phone" name="phone">
</div>
<div>
<input type="radio" id="ios" name="os" value="ios" checked />
<label for="ios" class="text">
<span>iOS</span>
</label>
<input type="radio" id="android" name="os" value="android" />
<label for="android" class="text">
<span>Android</span>
</label>
</div>
<button type="submit" class="btn btn-primary">送出</button>
</form>
</div>
</body>
jQuery.validator.methods.matches = function (value, element, params) {
var re = new RegExp(params);
return this.optional(element) || re.test(value);
};
var re = new RegExp("^09\\d{8}$");
$( "#myform" ).validate({
rules: {
phone: {
required: true,
matches: re, // <-- no such method called "matches"!
},
os: {
required: true,
}
},
messages: {
phone: {
required: "請填寫您的手機號碼",
matches: "您的手機號碼格式錯誤,需為10位的正確數字手機格式",
},
os: {
required: "請填寫您的手機系統",
},
},
submitHandler: function (form) {
alert('成功送出!');
}
});