As a form of input validation I need to coerce a string like '9>6'
to evaluate to a boolean value.
Apart from evaluating the string I can't seem to find a workaround .
I've always heard about the evilness of eval (especially since I'm validating a form input), about the fact that it could evaluate any script and performance issues.
but....
Are there any alternative in my case (dealing with relational operators)?
var arr = ['<9', '>2'];
var check = function (a) {
return arr.every(function (x) {
var string = '';
string += a + x;
try {
return eval(string);
} catch (e) {
return false;
}
});
};
console.log(check('3'))