[javascript] numbers not allowed (0-9) - Regex Expression in javascript

I want a regex pattern which will allow me any chars, but it will not allow (0-9) numbers?

This question is related to javascript regex

The answer is


Something as simple as [a-z]+, or perhaps [\S]+, or even [a-zA-Z]+?


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, "");


Like this: ^[^0-9]+$

Explanation:

  • ^ matches the beginning of the string
  • [^...] matches anything that isn't inside
  • 0-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/