我正在尝试使用JQuery Validate plugin验证表单。我需要验证巴西手机,但我的表格尚未经过验证。它只检查是否需要现场电话,但不是检查我的自定义规则。 我该如何解决这个问题?
这是我的代码:
<form action="/account/payment/create" name="payment_form" id="payment_form">
<input placeholder="Telefone" type="phone" name="phone" id="phone" class="form-control input-lg m-b" required>
<input type ="submit" class="btn btn-primary"> Pagar </input>
</form>
<script>
jQuery.validator.addMethod('celular', function (value, element) {
value = value.replace("(","");
value = value.replace(")", "");
value = value.replace("-", "");
value = value.replace(" ", "").trim();
if (value == '0000000000') {
return (this.optional(element) || false);
} else if (value == '00000000000') {
return (this.optional(element) || false);
}
if (["00", "01", "02", "03", , "04", , "05", , "06", , "07", , "08", "09", "10"].indexOf(value.substring(0, 2)) != -1) {
return (this.optional(element) || false);
}
if (value.length < 10 || value.length > 11) {
return (this.optional(element) || false);
}
if (["6", "7", "8", "9"].indexOf(value.substring(2, 3)) == -1) {
return (this.optional(element) || false);
}
return (this.optional(element) || true);
}, 'Informe um celular válido');
$("#payment_form").validate({
rules: {
phone: {
required: true,
celular: true
}
},
submitHandler: function(form) {
form.submit();
}
});
</script>