Javascript check if string contains only numbers and comma

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

How do you check if a string contains only numbers JavaScript?

To check if a string contains numbers in JavaScript, call the test[] method on this regex: /\d/ . test[] will return true if the string contains numbers. Otherwise, it will return false .

How do I check if a string only contains numbers?

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.

How do you check if a string contains only alphabets and numbers in JavaScript?

The RegExp test[] Method To check if a string contains only letters and numbers in JavaScript, call the test[] method on this regex: /^[A-Za-z0-9]*$/ . If the string contains only letters and numbers, this method returns true . Otherwise, it returns false .

How do I check if a string contains a comma?

“check if string has comma php” Code Answer.
php function strpos return position of string if it's not false or -1 then your string contain character..
$searchForValue = ',';.
$stringValue = '115,251';.
if[ strpos[$stringValue, $searchForValue] !== false ] {.
echo "Found";.

Chủ Đề