2012-02-07 18 views
0

可能重複:
jQuery validate phone number with with RegEx有效數據

我們必須允許沿文本字段只有10位。 () - 也。 (美國電話號碼格式。)當我在此文本字段中輸入數據時,它不應該允許第11位數字。如果我刪除所有數據,它將再次允許我輸入10位數字。 此字段也用於輸入電子郵件登錄。 對於電子郵件和手機號碼相同的文本字段。 請幫我出去做。

+1

您在http://stackoverflow.com/questions/5692232/jquery-validate-phone-number-with-with-regex 有你的答案 快樂編碼! – Ravia 2012-02-07 11:17:18

+0

嘗試在文本框更改事件中檢查文本框中的輸入。如果它有@符號,則允許其他方式將其限制爲10位長度。你可以使用下面的代碼。 $('。target')。change(function(){ alert('Handler for .change()called。'); }); – 2012-02-07 11:42:47

回答

0

您可以在html輸入元素上使用maxlength屬性。是這樣的:

<input type="text" name="usrname" maxlength="10" /> 

您還可以通過使用IF語句用於在同一輸入元素的電子郵件和美國的電話號碼格式的正則表達式。

0

使用jquery驗證輸入。

$(function() { 
      $('#yourtextboxid').change(function() { 
       //call a function that will check for @ sign into the text. If @ exists, //then allow user's input alse restrict it to 10 digit length. 
      }); 
     }); 

您可以檢查長度

$("#call_number").val().length === 10