Page 1 of 1

forms class question: Validation of Regex vs. Empty fields

Posted: Tue Jul 27, 2004 11:19 am
by tripps
I am trying to figure out which Validation scheme takes precedent: ValidateRegularExpression or ValidateAsNot Empty. See the following example:

Code: Select all

$registration->AddInput(array(
	"TYPE"=>"text",
	"NAME"=>"unitaddress1",
	"ID"=>"unitaddress1",
	"TABINDEX"=>13,
	"SIZE"=>40,
	"MAXLENGTH"=>100,
	"ValidateOnlyOnServerSide"=>1,
	"ValidateRegularExpression"=>"^[0-9A-Za-z ''-]+$",
	"ValidateRegularExpressionErrorMessage"=>"Please enter a valid street address.",
	"ValidateAsNotEmpty"=>1,
	"ValidateAsNotEmptyErrorMessage"=>"Please enter your street address.",
	"LABEL"=>$registrationfields['unitaddress1']
));
$registration->AddInput(array(
	"TYPE"=>"text",
	"NAME"=>"unitaddress2",
	"ID"=>"unitaddress2",
	"SIZE"=>40,
	"TABINDEX"=>14,
	"MAXLENGTH"=>100,
	"ValidateOnlyOnServerSide"=>1,
	"ValidateRegularExpression"=>"^[0-9A-Za-z ''-]+$",
	"ValidateRegularExpressionErrorMessage"=>"Please enter a valid street address.",
	"LABEL"=>$registrationfields['unitaddress2']
));
As you can see, my form contains two address lines. The first line must have something in it AND it must contain valid characters ("^[0-9A-Za-z ''-]+$"). The second line is not a required field, but if there is any data in it it must also contain valid characters. It appears however that since an empty field doesn't match the regex, it doesn't validate. As a workaround, I've changed the second field to use the following Regex:

Code: Select all

"^($|ї0-9A-Za-z ''\#-]+$)"
This does the trick for now, but I would like to know what the precedence various validation schemes have over one another. I suppose I could look through the code (already have added some cool new features to the form class, such as adding field specific validation error messages that appear with each field all at once), but if anyone knows the quick answer, that would be great. Thanks all!

Tripps

Posted: Tue Jul 27, 2004 11:39 am
by tripps
Sorry about that - I always thoroughly read the sticky posts and somehow missed the PHP quoting . . .

Re: forms class question: Validation of Regex vs. Empty fiel

Posted: Tue Jul 27, 2004 10:32 pm
by mlemos
tripps wrote:I am trying to figure out which Validation scheme takes precedent: ValidateRegularExpression or ValidateAsNot Empty.
Sorry that information was missing from the documentation. I just added it now to the Validate documentation and it now reads like this:

The validation of each input is performed according to the respective definition that was passed to the AddInput function. The order of the validation types is as follows:

* ValidateAsNotEmpty

* ValidateMinimumLength

* ValidateRegularExpression

* ValidateAsInteger

* ValidateAsFloat

* ValidateAsEmail

* ValidateAsCreditCard

* ValidateAsEqualTo

* ValidateAsDifferentFrom

* ValidateAsSet

* ValidationServerFunction

As a side note, the order of the client side validation types implemented by the Javascript code generated by the class is the same, except that, obviously, the ValidationClientFunction validation will be performed instead of ValidationServerFunction.

tripps wrote:As you can see, my form contains two address lines. The first line must have something in it AND it must contain valid characters ("^[0-9A-Za-z ''-]+$"). The second line is not a required field, but if there is any data in it it must also contain valid characters. It appears however that since an empty field doesn't match the regex, it doesn't validate.
If you set the ValidateOptionalValue parameter to an empty string, the regular expression validation will only be performed if the field value is not an empty string. Look at the credit card field definition of the main example script.