Reputation: 4202
I am trying to use RegExp validation for a number that can have up to 5 numbers followed up one option decimal place. Like 48293 or 23.4 are good. 99.99 or 453543 are not. I wrote the following function:
function validateLoad(load_value) {
var matchValue = new RegExp('[0-9]{1,5}(\.[0-9]{1})?')
return matchValue.test(load_value)
}
However, this seems to return true for all numerical values, can anyone tell me how to fix this?
Upvotes: 1
Views: 52
Reputation:
Using literal notation would avoid to escape backslashes :
function validateLoad(load_value) {
return /^\d{1,5}(\.\d)?$/.test(load_value)
}
Upvotes: 0
Reputation: 336478
You need to use anchors to make sure the entire string (and not just a substring) is matched by the regex. Also, don't forget to double the backslashes if you construct the regex from a string (and drop the {1}
, it's a no-op):
var matchValue = new RegExp('^[0-9]{1,5}(\\.[0-9])?$');
Upvotes: 3