I'd like to use a regex to determine if a user-supplied value exists in a list of approved values, regardless of case. Here is a pared-down example of the current JavaScript code, which works to match "JAN", "Jan", and "jan"–but does not match "jAN", "jAn", etc:
var validateValue = function(field, patternName){
"use strict"; //let's avoid tom-foolery in this function
var pattern="";
switch(patternName)
{
case "MMM": //month names only
pattern=/^JAN|Jan|jan*$/;
break;
// other cases and default follow in real code
}
if ( (!field.value.length) || pattern.test(field.value) ){
//we're good (the field is blank or passes the regular expression test); remove field's error message, enable the submit button
}
else {
//problems; let's show the error message and put focus back on problem field, disable the submit button
}
};
I tried pattern=/^
(?i)JAN|Jan|jan*$/;
based on what I learned from "Case insensitive Regex without using RegexOptions enumeration", but that doesn't do the trick ("Uncaught SyntaxError: Invalid regular expression...")
What is the correct regular expression for evaluating if a value matches, case-insensitive, a list item?