Reputation: 125
I am fairly new to regular expressions and I am having a time getting one that I have formulated to work correctly. Below is the expression that I have formulated.
^((?!^1[0-6]*\.?[0-2][0-4]+)^(\d+))$
I am trying to build an expression that will verify a number greater than 16.24. The input needs to be able to accept whole numbers like 17 without a user having to put in 17.00 to verify. Any ideas of what I am doing wrong?
Upvotes: 0
Views: 722
Reputation: 9178
One way you could do this would be to use a regular expression to extract the numeric values then parse them to a number and compare them with the desired constant.
Javascript: Code Example
Numbers as strings:
var test = function(str){
return 16.24 < parseFloat(str);
};
console.log( test("234.23") == true ); // true
console.log( test("-234.23") == false ); // true
Numbers hidden within strings with other characters.
var test = function (str) {
var re,
num;
if (/[eE]/.test(str)) {
// search for scientific numbers
re = /-?\d+(\.\d+)?[eE]\d+/;
} else {
// search for whole or decimal numbers
re = /-?\d+(\.\d{1,2})?/;
}
num = str.match(re);
return 16.24 < parseFloat(num);
};
console.log(test("input = 234.23") == true); // true
console.log(test("input = 2e34") == true); // true
console.log(test("input = -2e34") == false); // true
console.log(test("input = -234.23") == false); // true
Upvotes: 1