Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6322747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:23:27+00:00 2026-05-24T16:23:27+00:00

I have a CMS app that allows custom fields to be added. One type

  • 0

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.

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.

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.

The regex pattern I’ve supplied for the PHP end of things is: /^([A-Za-z0-9 .\-']*)$/ 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 preg_match in php but using string.match(pattern); in javascript is failing every time, no matter what input I give it.

Here is a snippet of 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+').');
                }
            }
        }

}

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…

Sorry for the long-winded explanation, what am I doing wrong with this regex matching?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:23:27+00:00Added an answer on May 24, 2026 at 4:23 pm

    I’m guessing that your regular expression is a string like this:

    "/^([A-Za-z0-9 .\-']*)$/"
    

    and that won’t work as a regular expression. Try removing the leading and trailing slashes before you insert it into your JavaScript.

    Open up your JavaScript console and see what this does to see the difference some slashes can make:

    var pat = "^([A-Za-z0-9 .\\-']*)$";
    console.log('pancakes'.match(pat));
    console.log('pancakes'.match(new RegExp(pat)));
    console.log('pancakes'.match(/^([A-Za-z0-9 .\-']*)$/));
    
    var pat2 = "/^([A-Za-z0-9 .\\-']*)$/";
    console.log('pancakes'.match(pat2));
    console.log('pancakes'.match(new RegExp(pat2)));
    

    Or go here: http://jsfiddle.net/ambiguous/7FPux/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a CMS that supports multiple sites, one of our features allows our
I'm building a CMS in PHP and one dread I have is that the
I have a rails CMS app that manages content for a production web server
I have a CMS that uses a syntax based on HTML comments to let
At my current job we have a CMS system that is .NET/SQL Server based.
We have a hosted site that has a CMS we built running on a
I have an application that I would like to embed inside our companies CMS.
I have a single Rails 2.2.2 app that I want to 'share' with multiple
For a new SaaS app that I'm working on, I want the end user
Im trying to use this approach. My app is a cms. I have admins,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.