Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
186
rated 0 times [  189] [ 3]  / answers: 1 / hits: 21781  / 13 Years ago, mon, may 23, 2011, 12:00:00

I use the regular expression /^+(90)[2-5]{1}[0-9]{9}$/ for phone validation, but when someone enters any special characters (such as * - / ( ) - _) in the input, I want to replace the characters with an empty string (remove them). Note that I don't want to replace the +.


How can I do that?


More From » regex

 Answers
139

This will remove all non-numeric characters in a given string:



myString = myString.replace(/D/g,);


D matches anything that isn't a number; d matches a number.






Misread the question. To remove all non-numeric characters except +, do:



myString = myString.replace(/[^d+]/g,);

[#92102] Saturday, May 21, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
justynkaina

Total Points: 742
Total Questions: 83
Total Answers: 102

Location: Hong Kong
Member since Tue, Oct 19, 2021
3 Years ago
;