PHP Classes

Validation of optional Fields

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  All threads  >  Validation of optional Fields  >  (Un) Subscribe thread alerts  
Subject:Validation of optional Fields
Summary:How can you validate an optional field for eg. Email Adress
Messages:2
Author:Ludwig Ruderstaller
Date:2005-09-06 12:30:22
Update:2005-09-06 14:52:27
 

  1. Validation of optional Fields   Reply   Report abuse  
Picture of Ludwig Ruderstaller Ludwig Ruderstaller - 2005-09-06 12:30:22
Hi,

First I want to thank you for your class. I extended it a bit to work better with smarty. (generating the smarty template on the fly with a "stream" resource.)

I have a lot of forms which have fields which are optional. But if something is entered in this fields they should be validated. I didnt find anything in the documentation.

For example (taken from your test_form.php)

$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"email",
"ID"=>"email",
"MAXLENGTH"=>100,
"Capitalization"=>"lowercase",
"ValidateAsEmail"=>1,
"ValidationErrorMessage"=>"It was not specified a valid e-mail address",
"LABEL"=>"<u>E</u>-mail address",
"ACCESSKEY"=>"E"
));

This field has to be inserted a valid emailadress. Its not valid if its empty. So i want to make a field which is optional, but has to be a valid email if text is entered.

tia
Rufinus

  2. Re: Validation of optional Fields   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2005-09-06 14:52:27 - In reply to message 1 from Ludwig Ruderstaller
That is a FAQ, despite it is documented and demonstrated in the main example script for the credit card field.

All you need to do is to set the parameter ValidateOptional value to an empty string ("") so the class makes the validation code accept that value without further validation.