Compare Values with Arbitrary Comparison Operator

JavaScript for comparing values when the operator to use is variable.

Posted Dec 21, 2012
Last Updated Dec 21, 2012

This free javascript will allow you to compare two values with variable comparison operators. You might do this if you have a form where the user can choose whether to compare two other fields based on different comparison conditions. For example, if you have a list of pictures to display based on whether they were published on or before a user supplied date, or the user can choose to show ones on or after (where the on before/on after is the variable).

This javascript function condenses logic so that you don't have to create a bunch of greater than/less than blocks in your code. I have tested the function in IE and Firefox and presume it works in all browsers. Use it at your own risk (not that there is any forseeable problem). Please provide feedback if you enjoy the script or find problems.

Here is a set of examples:

Example Form

Numeric Comparison with jQuery Spinner widget ? True
Numeric Comparison with standard text input ? False
Alpha-Numeric Comparison with Select and Text Input ? True
Alpha-Numeric Comparison with Selects ? False

Code

Here is the code. Please include the comment.

function smo_compare_values(number,against,symbol,type){ /* Function Written by Shawn Olson http://www.shawnolson.net Full Docs at http://www.shawnolson.net/a/1822/compare_values_with_arbitrary_comparison_operator.html */ if (number == undefined || against == undefined || symbol == undefined) { return false; } if (type != undefined && type=="num") { number = number * 1; against = against * 1; } switch(symbol){ case 'gte': return (number>=against); case 'lte': return (number<=against); case 'e': return(number==against); case 'lt': return (number<against); case 'gt': return (number>against); default: return false; } }

This function will take a supplied value (number) and compare it against another value (against).

The comparison operator used is determined by the string (symbol).

Possible comparison symbols: gte, lte, gt, lt, e.

If you need the comparison to be numeric, pass a fourth parameter as "num". If you fail to do this, it can cause problems.

If any input is undefined OR the symbol is not in the allowed list, the function returns false. Otherwise the function returns true if number compares to the against value based on the comparison operator.

Numeric Comparison Examples:

smo_compare_values(4,5,'gte','num'); // false
smo_compare_values(4,5,'lte','num'); // true
smo_compare_values(5,5,'lte','num'); // true
smo_compare_values(4,5,'gt','num'); // false
smo_compare_values(4,5,'lt','num'); // true
smo_compare_values(4,5,'e','num'); // false
smo_compare_values(5,5,'e','num'); // true

Alpha-Numeric Comparison Examples:

smo_compare_values('Andrew','Zackary','gte'); // false
smo_compare_values('Andrew','Zackary','lte'); // true
smo_compare_values('Zackary','Zackary','lte'); // true
smo_compare_values('Andrew','Zackary','gt'); // false
smo_compare_values('Andrew','Zackary','lt'); // true
smo_compare_values('Andrew','Zackary','e'); // false
smo_compare_values('Zackary','Zackary','e'); // true

Comment

No HTML Tags are permitted.

Angry Teapot Level Design Awards

Newsletter Subscription