1

I try to validate various input strings. Therefore I tried to use regular expressions. Unfortunately all negativ cases match to true as well. How can I sepcify that the regular expression has to match the whole string?

const regex = /(^([0-9]+(.||,)+[0-9]?)$)||(^([0-9]+)$)||(^(.||,)+[0-9]+$)/;

const testSetPositiv = ["34", "45.45", ".7867", "3434,34", ",7834", "546.", "45546,", "0"]

const testSetNegativ = ["wegrger", "3443,34erf", "3443.34erf", ",", ".", "4525tg", "1,211.23", "1.211,23"]


testSetPositiv.forEach(function(value) {
  console.log(regex.test(value));
  //should always return true
})
console.log("----------------");

testSetNegativ.forEach(function(value) {
  console.log(regex.test(value));
  //should always return false
})


I also tried to use parseFloat(...), but it accepts for example "5.45abc":

const testSetPositiv = ["34", "45.45", ".7867", "3434,34", ",7834", "546.", "45546,", "0"]

const testSetNegativ = ["wegrger", "3443,34erf", "3443.34erf", ",", ".", "4525tg", "1,211.23", "1.211,23"]


testSetPositiv.forEach(function(value) {
  console.log(parseFloat(value) >= -Number.MAX_VALUE)
  //should always return true

})
console.log("----------------");

testSetNegativ.forEach(function(value) {
  console.log(parseFloat(value) >= -Number.MAX_VALUE)

  //should always return false
})

4
  • are digits with comma valid? Commented May 26, 2018 at 16:24
  • @NinaScholz yes, should be valid in this case; e.g., "123,123" true, BUT separators shouldn't be accepted; e.g., "1.000,23" false, "1,211.23" false Commented May 26, 2018 at 16:28
  • are you doing this because the number values are all strings ? (the items in array with quotes will become strings). if the number values in the array did not have strings then they would be considered numbers. E.g. [4,66,"66","4"] is 2 numbers and 2 strings Commented May 26, 2018 at 16:35
  • yes they are all strings Commented May 26, 2018 at 16:38

5 Answers 5

1

A simple regex solution:

const regex = /^(\d*[.,]?\d+|\d+[.,]?\d*)$/;

const testSetPositiv = ["34", "45.45", ".7867", "3434,34", ",7834", "546.", "45546,", "0"]

const testSetNegativ = ["wegrger", "3443,34erf", "3443.34erf", ",", ".", "4525tg", "1,211.23", "1.211,23"]



testSetPositiv.forEach(function(value) {
  console.log(regex.test(value));
  //should always return true
})
console.log("----------------");

testSetNegativ.forEach(function(value) {
  console.log(regex.test(value));
  //should always return false
})

Sign up to request clarification or add additional context in comments.

Comments

0

parseInt and parseFloat can return numbers even if the string contains letters, check snippet:

const testSetNegativ = ["wegrger", "3443,34erf", "3443.34erf", ",", ".", "4525tg", "1,211.23", "1.211,23"];
testSetNegativ.forEach(function(value) {
  console.log(parseFloat(value));
})

I suggest you to use:

  • .replace(',', '.') to replace the commas (used in Europe as decimal separator) with the correct JavaScript decimal separator,
  • Unary operator + to easily convert to number. (It will return NaN if not a number)

const testSetPositiv = ["34", "45.45", ".7867", "3434,34", ",7834", "546.", "45546,", "0"]
const testSetNegativ = ["wegrger", "3443,34erf", "3443.34erf", ",", ".", "4525tg", "1,211.23", "1.211,23"];

testSetPositiv.forEach(function(value) {
  console.log(+(value.replace(',', '.')) >= -Number.MAX_VALUE)
  //should always return true
})
console.log("----------------");
testSetNegativ.forEach(function(value) {
  console.log(+(value.replace(',', '.')) >= -Number.MAX_VALUE)
  //should always return false
})

Hope it helps.

8 Comments

Sorry, I missed some negative examples, which might be not covered with this approach. I updated the question with th e negative examples; e.g., "1,211.23", "1.211,23" should be both evaluate to false
@d4rty Are you using , as decimal or thousand separator? Anyway, I've updated my snippets!
No they shouldn't be accepted (false input), any kinfd of thousand separator
@d4rty If they shouldn't be accepted, why some of them are in the testSetPositiv ?
Which string do you exaclty mean in the positive set?
|
0

You can use the Number class to validate a string is a valid number. If it is not a valid number, it will return NaN

Example:

>Number(".23232")
0.23232
>Number(".23232abc")
NaN

Comments

0

use isNaN , (side note "45546," should not be a number)

        const testsetMixed = [34, 45.45, ".7867", "wegrger", "3443,34erf", "546.", "45546,", "0"]


    testsetMixed.forEach(function(value){
       if( isNaN(value)) {
         console.log('not number');
       } else {
         console.log('number');
       }

    })

Comments

0

Can use isNan() which returns true if the input string is not numeric

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.