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:
Form Field entry with 12-characters whereby the very last character is not numeric (observe no error message in red) form can be submitted:
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:
"validation"
and "type"
."validation"
and "type"
within their respective array()s. It did not make it work."type"
to "validation"
. It did not make it work.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.