我想驗證電話號碼爲: (000)111-1111 我正在使用此代碼段,如果用戶只輸入數字,則工作正常。但如果他開始一個括號,所有崩潰..我真的需要幫助......jQuery驗證電話號碼
$("input#phone1,input#phone2").keyup(function() {
var curchr = this.value.length;
var curval = $(this).val();
//var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
var numericReg = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
if(!numericReg.test(curval)) {
$(this).prev('label').append(tag_error + numeric_chars_only + end_tag);
console.log($(this)+numeric_chars_only);
}
if (curchr == 3) {
$("input#phone1").val("(" + curval + ")" + " ");
} else if (curchr == 9) {
$("input#phone1").val(curval + "-");
}
});
「tag_error」,「numeric_chars_only」和「end_tag」定義在哪裏? – 2012-03-29 18:05:45
這只是提醒用戶.. – 2012-03-29 18:06:29
嗨!嗯,我已經爲你創建了這個 - http://jsfiddle.net/5NCQg/18/你可以給我們演示一個演示你的意思是onkeyup驗證? :)此外http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery **或** http:// stackoverflow。 com/questions/6960596/jquery-for-phone-numbers-example-of-a-regular-expression可能會給你更好的想法!歡呼 – 2012-03-29 18:26:00