PHP Classes

regular expression

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  PHP Forms Class with HTML Generator and JavaScript Validation package blog  >  How to Show Google Ma...  >  All threads  >  regular expression  >  (Un) Subscribe thread alerts  
Subject:regular expression
Summary:Clarification on regular expressions
Messages:3
Author:mrbinky 3000
Date:2007-09-26 03:57:01
Update:2007-09-26 06:27:44
 

  1. regular expression   Reply   Report abuse  
Picture of mrbinky 3000 mrbinky 3000 - 2007-09-26 03:57:01
I am trying to validate a fax number. The fax number is optional. The field can be blank OR it can be a valid 10 digit phone number. I tried using the regular expression option.

$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"fax",
"ID"=>"fax",
"MAXLENGTH"=>12,
"LABEL"=>"<u>F</u>ax",
"ACCESSKEY"=>"F",
"TABINDEX"=>$ti++,
"ValidateRegularExpression"=>array(
'^$',
'[0-9]{3}-[0-9]{3}-[0-9]{4}'),
"ValidateRegularExpressionErrorMessage"=>array(
'',
"Please use a 10 digit fax number seperated by dashes.")
));

How can I make something valid if is blank OR matches the regular expression.

  2. Re: regular expression   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-09-26 05:12:27 - In reply to message 1 from mrbinky 3000
I am not sure if regular expressions can match empty values. Anyway, you can always use the ValidateOptionalValue parameter set to an empty string to make the validation accept empty strings without further validation.

  3. Re: regular expression   Reply   Report abuse  
Picture of mrbinky 3000 mrbinky 3000 - 2007-09-26 06:27:44 - In reply to message 2 from Manuel Lemos
Thanks. I set ValidateOptionalValue to '' and the field now accepts blank values OR valid 10 digit fax numbers. Here is the code for those interested

$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"fax",
"ID"=>"fax",
"MAXLENGTH"=>12,
"LABEL"=>"<u>F</u>ax",
"ACCESSKEY"=>"F",
"TABINDEX"=>$ti++,
"ValidateOptionalValue"=>'',
"ValidateRegularExpression"=>array('[0-9]{3}-[0-9]{3}-[0-9]{4}'),
"ValidateRegularExpressionErrorMessage"=>array("Please use a 10 digit fax number separated by dashes."),
));