I have a telephone input field. You should only type numbers, spaces or leave it empty.
An idea how to build the regex?
I have a telephone input field. You should only type numbers, spaces or leave
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The regex would be
but I would not do it this way. You should allow all characters, then filter out the numbers in a second step, using something like
result = subject.replace(/\D+/g, "");.Reason: People have all kinds of ways to enter phone numbers (
1-(123) 343-2345etc.), and they don’t like it if a website tells them the number is incorrect.Are you aware that you’re excluding country codes with this requirement? I have all my phone numbers stored as
+49 123 456-7890, and I wouldn’t like my country code to be mistaken for an area code.