Search the Community
Showing results for tags 'regular expression'.
-
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!
- 3 replies
-
- regular expression
- test method
-
(and 1 more)
Tagged with:
-
Hi, I have a form field that needs to be validated before any attempt is made to upload the contents to a database. Fear not, the test is performed again in PHP before the db update! I am using jQuery and the associated jQuery form validation module. Everything works fine but I have found that three slightly different regular expression tests can be used - my question is: are there any performance or other reasons why one particular approach is best? Here's the code together with an explanation of the regex: $.validator.addMethod( "details_OK", function (value, element) { var valx = String(value); // var regex_tester = new RegExp( var regex_tester = /^(http:\/\/)(\S)+(\?u=){1}(\S)+(\&id=){1}(\S)+(\&e=){1}(\S)+/; /* var regex_tester = / // must start with what follows "^" + // now the http bit... "(http:\/\/)" + // any letters, numbers or periods (one or more of them) "(\\S)+" + // must contain ?u= (once) "(\?u=){1}" + // now more letters, numbers etc.. "(\\S)+" + // now &id= (once) "(\&id=){1}" + // now more letters, numbers etc.. "(\\S)+" + // now &e= (once) "(\&e=){1}" + // now more letters, numbers etc.. "(\\S)+" + /; */ // Note: all 3 of the following tests work OK... // if (regex_tester.test(valx)) // if (/^(http:\/\/)(\S)+(\?u=){1}(\S)+(\&id=){1}(\S)+(\&e=){1}(\S)+/.test(valx)) if (valx.match(/^(http:\/\/)(\S)+(\?u=){1}(\S)+(\&id=){1}(\S)+(\&e=){1}(\S)+/)) return true; else return false; }, "Error - The Internet address (URL) does not have some of the required/expected details." ); // end details check I could not get the "...new RegExp approach to work thus it is commented out. Any thoughts/advice will be most appreciated. Thanking you in anticipation, Cheers from Oz.
-
Hi, I was learning the code on creating a search engine, that accompanies this book from here: http://www.peachpit.com/articles/article.aspx?p=1802754&seqNum=4 and encountered an issue in the se_index.php file. Everything worked fine until I tryed cyrillic characters, the preg_match_all('/\b\w+\b/', $content, $output) function didn't allow them to get through: After researching the net I fount the solution adding the u caracter for unicode like this: preg_match_all('/\b\w+\b/u', $content, $output); and it worked on the localhost with LAMPP. Recently I tried to index a website on a shared hosting that runs PHP-5.2.17, and the same method that worked on the localhost, didn't with cyrillic there. I also tryed preg_match_all('/\b\[a-zA-Z\p{Cyrillic}0-9]+\b/u', $content, $output); and other combinations of regular expressions with the \p{Cyrillic} but nothing worked so far. If anybody has knowlege how to solve this issue, please give me a note, thanks.
- 5 replies
-
- cyrillic
- regular expression
-
(and 1 more)
Tagged with: