简体   繁体   English

在Java中使用正则表达式进行电话号码验证

[英]Phone number validation using regex in java

I am new to Java and Regex. 我是Java和Regex的新手。

I have to do simple phone number validations. 我必须进行简单的电话号码验证。

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

Here user can't entered more than 10 characters. 在此用户不能输入超过10个字符。 but i need, if user try to enter string values should not allow. 但我需要,如果用户尝试输入字符串值应该不允许。 Only numbers should allow.and no space in between numbers also should not allow. 只能有数字,数字之间也不能有空格。 Please help me how can achieve with reqex. 请帮助我如何用reqex实现。

You may use 您可以使用

<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" will handle numeric input onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"将处理数字输入
  • maxlength="10" - will set the max symbol length. maxlength="10" -将设置最大符号长度。

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM