I have created a function that checks to see if a input text field is valid and then either append a class name to the element that will correspond to some css code or do nothing if it is valid.
function checkInputText(inputType, elem, oldClass, classError) {
var inputT = document.getElementById(elem);
var oldClass = oldClass;
var b = regex[inputType].test(inputT.value)
if (!b) {
inputT.className = classError + ' ' + oldClass;
return false;
} else {
inputT.className = oldClass;
}
}
However, for some unknown reason, when the function calls the .test() function it returns true for an event the first time, but when you try it again it returns false.
The full code is here http://jsfiddle.net/assuredlonewolf/cscyB/2/
“As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.” https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test
You can work around this behavior by either resetting the
lastIndexto 0 or creating a new regex each test.