2016-10-20 55 views
0

我是Java和Regex的新手。在java中使用正則表達式的電話號碼驗證

我必須做簡單的電話號碼驗證。

<input type="tel" maxlength="10"> 

此處用戶不能輸入超過10個字符。但我需要,如果用戶嘗試輸入字符串值不應該允許。只有數字應該允許,數字之間也不應有空格。請幫助我如何才能實現與reqex。

+0

''。還是你想禁止從鍵盤輸入非數字? –

+0

是的,我想禁止從鍵盤輸入非數字。 – Ravikanth

+1

Try < = 48 && event.charCode <= 57」type =「number」maxlength =「 10「/>' - 不知道你需要什麼類型,試着用'type =」number「'和'type =」tel「' –

回答

1

您可以使用

<input onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57" type="number" maxlength="10" /> 
  • onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"將處理數字輸入
  • maxlength="10" - 將設置最大符號長度。
-1

試試這個

<input type="number" onKeyDown="if(this.value.length==10) return false;" /> 
+0

但是我們必須限制用戶不要輸入String。 maxLength =「10」對長度來說是完美的。 – Ravikanth

+0

這裏它不允許輸入字符串也..只允許輸入數字 –

+0

雖然這段代碼可以解決問題,[包括解釋](http://meta.stackexchange.com/questions/114762/解釋完全基於代碼的答案)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – andreas

-1
<input type="number" maxlength="10"> 
+0

這不適用於IE Edge瀏覽器。 – Ravikanth

相關問題