numbers not allowed (0-9) - Regex Expression in javascript
I want a regex pattern which will allow me开发者_StackOverflow社区 any chars, but it will not allow (0-9) numbers?
Simply:
/^([^0-9]*)$/
That pattern matches any number of characters that is not 0
through 9
.
I recommend checking out http://regexpal.com/. It will let you easily test out a regex.
Like this: ^[^0-9]+$
Explanation:
^
matches the beginning of the string[^...]
matches anything that isn't inside0-9
means any character between 0 and 9+
matches one or more of the previous thing$
matches the end of the string
\D
is a non-digit, and so then \D*
is any number of non-digits in a row. So your whole string should match ^\D*$
.
Check on http://rubular.com/r/AoWBmrbUkN it works perfectly.
You can also try on http://regexpal.com/ OR http://www.regextester.com/
It is better to rely on regexps like ^[^0-9]+$
rather than on regexps like [a-zA-Z]+
as your app may one day accept user inputs from users speaking language like Polish, where many more characters should be accepted rather than only [a-zA-Z]+
. Using ^[^0-9]+$
easily rules out any such undesired side effects.
You could try something like this in javascript:
var regex = /[^a-zA-Z]/g;
and have a keyup event.
$("#nameofInputbox").value.replace(regex, "");
Something as simple as [a-z]+
, or perhaps [\S]+
, or even [a-zA-Z]+
?
精彩评论