Ravi K Chowdary Ravi K Chowdary - 1 month ago 13
Java Question

Phone number validation using regex in java

I am new to Java and Regex.

I have to do simple phone number validations.

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


Here user can't entered more than 10 characters. 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.

Answer

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
  • maxlength="10" - will set the max symbol length.
Comments