javascriptphpregexvalidform

Why is ValidForm ignoring my validation expression?


The code below properly prevents the form from being submitted if anything between 1-11 characters is entered into the form field.

However, once 12-characters of any type are entered, the form field passes validation.

  $objGroup->addField('phone', 'Location Phone', VFORM_CUSTOM,
    array(
      'required' => false,
      'validation' => '/^\d{3}-\d{3}-\d{4}$/',
      'minLength' => 12,
      'maxLength' => 12
    ),
    array(
      'type' => 'Location Phone not entered correctly',
      'minLength' => 'Location Phone must be entered like ###-###-####'
    ),
    array(
      'hint' => '###-###-####',
      'tip' => 'Enter your phone number.',
      'default' => $GLOBALS['phone']
    )
  );

I need it to only permit a value string of 999-999-9999 whenever something is entered into the form field. What is incorrect with my code?

Form Field entry with 11-characters (observe the error message in red) form cannot be submitted:

enter image description here

Form Field entry with 12-characters whereby the very last character is not numeric (observe no error message in red) form can be submitted:

enter image description here

Except from VFB Documentation

#### Custom field types
* `VFORM_CUSTOM`
    This generates a text input field with a custom validation regular expression 

* `VFORM_CUSTOM_TEXT`
    This generates a textarea input field with a custom validation regular expression

##### Example - Validating a social security number
    $objSocialSecurity = $objForm->addField(
        "socialsecurity", 
        "Your social security number",
        VFORM_CUSTOM,
        array( 
            "validation" => "/^\d{3}-\d{2}-\d{4}$/"
        ),
        array(
            "type" => "Invalid Social Security number"
        )
    );

My Trial & Error Tests:

  1. copied and pasted the sample validation snippet from the documentation I found, verbatim. It did not make it work. My suspicion is there is some sort of disconnect between "validation" and "type".
  2. changed my use of single-quotes to double-quotes as shown in the documentation. It did not make it work.
  3. re-ordered the position of "validation" and "type" within their respective array()s. It did not make it work.
  4. changed "type" to "validation". It did not make it work.
  5. removed use of minLength and maxLength to see if there is a conflict. It did not make it work.
  6. made the field required to see if the validation only works if form field is required. It did not make it work.
  7. I've exhausted my t&e tests...

Solution

  • Try removing the ^ and $ chars from your regex:

    /\d{3}-\d{3}-\d{4}/

    With a minLength and maxLength of 12, ^ and $ become superfluous, and can often cause validation problems of their own accord.