I have an input field that I will only accept numbers, commas and periods. How can I test if the string is valid according to these rules?
I have tried the following:
var isValid = /[[0-9][,][.]]$/.test[str];
but it's not working. isValid variable is always false.
asked Feb 20, 2014 at 12:12
3
Your regexp expects one character from the first class [0-9], then one from the second class [comma] then one from the last class [dot]. Instead you want any number of characters [*] from the class containing digits, commas and dots [[0-9,.]
]. Also, you don't need the parenthesis:
var isValid = /^[0-9,.]*$/.test[str];
DEMO [and explanation]: //regex101.com/r/yK6oF4
answered Feb 20, 2014 at 12:17
TibosTibos
27.1k4 gold badges48 silver badges62 bronze badges
2
var regex = "[-+]?[0-9]*\.?[0-9]*"
This works perfect for decimal number & Integer. e.g.
1 - true
1.1 - true
1.1.1 - false
1.a - false
answered Aug 9, 2016 at 16:38
Check if String contains only Digits #
Use the test[]
method to check if a string contains only digits, e.g. /^[0-9]+$/.test[str]
. The test
method will return true
if the string contains only digits and false
otherwise.
Copied!
function onlyNumbers[str] { return /^[0-9]+$/.test[str]; } console.log[onlyNumbers['1234']]; // 👉️ true console.log[onlyNumbers['123hello123']]; // 👉️ false console.log[onlyNumbers['123.5']]; // 👉️ false
If you need to match decimal or comma-separated numbers, scroll down to the next code snippet.
We used the RegExp.test method to check if a string contains only digits.
The method returns true
if the regular expression is matched in the string and false
otherwise.
The forward slashes / /
mark the beginning and end of the
regular expression.
The caret ^
matches the beginning of the input, and the dollar sign $
matches the end of the input.
The part between the square brackets []
is called a character class and matches a range of digits from 0
to 9
.
The plus +
matches the preceding item [the 0-9
range] 1 or more times.
If you ever need help reading a regular expression, bookmark this regex cheatsheet from MDN.
If you need to also match decimal or comma-separated numbers, add the character you'd like to match between the square brackets []
.
Copied!
function onlyNumbers[str] { return /^[0-9.,]+$/.test[str]; } console.log[onlyNumbers['1234']]; // 👉️ true console.log[onlyNumbers['123hello123']]; // 👉️ false console.log[onlyNumbers['123.5']]; // 👉️ true console.log[onlyNumbers['123,5']]; // 👉️ true
We added a dot and a comma between the square brackets []
to also match these characters.
If your use case requires other characters, simply add them between the square brackets.
Note that the 0-9
range can also be written using the \d
[digit] special character.
Copied!
function onlyNumbers[str] { return /^\d+$/.test[str]; } console.log[onlyNumbers['1234']]; // 👉️ true console.log[onlyNumbers['123hello123']]; // 👉️ false
The \d
special character is equivalent to matching any digit using [0-9]
, but I find the range to be more readable and intuitive.
Further Reading #
- Check if String contains only Letters in JavaScript
- Remove all non-alphanumeric Characters from String in JS