Compare Values with Arbitrary Comparison Operator
JavaScript for comparing values when the operator to use is variable.
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
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
- Free Useful Javascripts
Links to articles that give free javascript code or javascript examples.
- Altering CSS Class Attributes with JavaScript
- Change HTML Styles with JavaScript
- Select Some Checkboxes JavaScript Function
- Select All Checkboxes in a Form with JavaScript
- Get a Form Element's Label with JavaScript
- Select Radio Inputs JavaScript
- UseMaps Crash IE When Changed
- Hide Select Menus JavaScript
- Manipulating Element Styles Based on CSS Classes Using Prototype
- Compare Values with Arbitrary Comparison Operator
- Related Topics