Note that there are some explanatory texts on larger screens.

plurals
  1. POperl regex pattern works in php but not in javascript?
    primarykey
    data
    text
    <p>I have a CMS app that allows custom fields to be added. One type of custom field is a user text input box. When creating this field type you can also supply a perl regex for use in the preg_match php function to validate user input for this field.</p> <p>Now I'm setting up new way of administrating the data with lots of front end jquery fanciness and I'm running into this issue. When the form is generated dynamically (ajax/php) a JSON string is also assembled and added to the ajax repsonse data for along with the dynamic form HTML. The receiving javascript function paints the html and parses the JSON string that contains information on the fields (what type of field it is: text or select along with other field options present for each field. This JSON object is then attached to the jQuery data() method of the #submitButton of the form.</p> <p>When the user submits the form, the data() keys of the button are looped to access the information about which form id's should be checked for input validation, ie selectindex cant be zero and text fields must validate against the supplied regex pattern.</p> <p>The regex pattern I've supplied for the PHP end of things is: <code>/^([A-Za-z0-9 .\-']*)$/</code> and this has been working great in php, and the rest of what I described is working great as well. My problem is that I'm really not good at regex and I do not uderstand why this pattern matches correctly via <code>preg_match</code> in php but using <code>string.match(pattern);</code> in javascript is failing every time, no matter what input I give it.</p> <p>Here is a snippet of code:</p> <pre><code>var checklist = $('#ficb1').data(); // gets data() object. var errors = new Array(); for(var key in checklist) { // loop through data() keys // verify text field if(checklist[key]["type"] == "text") { // do the following for text fields if(checklist[key]["options"]) { // checks if a pattern is set // found pattern! regex check! var pattern = checklist[key]["options"]; // verfied value: /^([A-Za-z0-9 .\-']*)$/ var valstr = $('#'+key).val(); // key = id of field being checked without # sign valstr varified to hold the value of the input text. if(!valstr.match(pattern)) { // add error message to error array - regex match not found. var engkey = key.replace('_', ' '); errors.push(ucfirst(engkey.substr(1)) + ' did not match the required pattern ('+pattern+').'); } } } } </code></pre> <p>No matter what I input, the function ends up adding that text to the error erray, even when I enter in "hello" without quotes, which should match that regex...</p> <p>Sorry for the long-winded explanation, what am I doing wrong with this regex matching?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload