angelsonearth Posted December 21, 2014 Share Posted December 21, 2014 Hello there! Please kindly note that I've been staring down this code for days now, and I can't seem to get why the regular expression test method doesn't work (and why Firebug says "it is not a function" when my code skips over everything that deals with the results of the test method). I am trying to create a utility function for form validation that passes in an id and a regular expression. Here is my calling code: regularExpressionMatchFunction("firstName", "/^[A-Za-z'\.]+\s*[A-Za-z'\.\s]*$/"); Here is the code with the erroneous component in bold: function regularExpressionMatchFunction(id, regularExpressionMatch) { //declare variables used in field check. alert("At beginning of function, the regular expression is " + regularExpressionMatch); var fieldName; var fieldNameValue var regExFieldName var finalRegExpressBooleanFieldName var fieldNameValueTrimmed // Get field name and field value fieldName = document.getElementById(id); fieldNameValue = fieldName.value; // TRIM WHITE SPACE Function Called here-- prepare code for trimming off white space on fieldName value fieldNameValueTrimmed = trimWhiteSpace(fieldNameValue); alert("My fieldNameValueTrimmed is" + fieldNameValueTrimmed); // Now for the regular expression, which looks for letters, apostrophes, and periods, although I don't need to escape the period (plus white space // Have to change the fieldNameValue to trimmed version above // HERE IS ERROR HERE IS ERROR HERE IS ERROR // NOT RECOGNIZED AS A FUNCTION finalRegExpressBooleanFieldName = regularExpressionMatch.test(fieldNameValueTrimmed); // EVERYTHING AFTER THE ABOVE CODE JUST GETS SKIPPED OVER, INCLUDING THE FOLLOWING ALERT MESSAGE alert("After doing regular expression MATCH, the value of finalRegExpressBooleanFieldName (the boolean) is " + finalRegExpressBooleanFieldName); // These alerts need to be replaced with calls to addErrorMessage and removeErrorMessage if (finalRegExpressBooleanFieldName == true) { //removeErrorMessage inserted here will take the element by id field but not an error message as arguments removeErrorMessage(id); alert("You are an awesome coder!"); } else { // addErrorMessage will take the getElementById field "firstName" and also the error message as arguments addErrorMessage(id, "The name value must include alphabetical letters (and can include apostrophes, periods, and spaces)"); alert("You code like toodles"); // The return false code didn't work in the addErrorMessage function itself, so I'll try it here return false; }} If anyone can help me figure out why the code breaks at the test method in bold above, I would be most appreciative! Thank you so very much! Link to comment Share on other sites More sharing options...
HartleySan Posted December 21, 2014 Share Posted December 21, 2014 It's because the second argument you're passing to the function is a string literal, not a regular expression literal. In PHP, you have to declare regexes as strings, but in JS, regexes have their own type (and should not be surrounded by quotes). You would only ever use a string for a regex in JS if you're using the RegExp constructor, which you're not. Short answer: If you want to do a regex literal, which I assume you do, take the quotes off the second parameter being passed to the function. That will fix that error. As for the error message you're getting, you're getting it because there is no test method for strings in JS, just regexes. 1 Link to comment Share on other sites More sharing options...
angelsonearth Posted December 29, 2014 Author Share Posted December 29, 2014 Dear Hartley Sans, You are certainly all heart, Hartley Sans, and very, very much appreciated! Between you and Larry Ullman, I think I stand a chance of becoming a serious Web coder one day! Thanks so much, and bless your souls! Link to comment Share on other sites More sharing options...
HartleySan Posted December 29, 2014 Share Posted December 29, 2014 It's nothing, really. I just have more familiarity with JS than most people. Anyway, enjoy the rest of your holidays. Link to comment Share on other sites More sharing options...
Recommended Posts