Page 1 of 1
Form Validation
Posted: Wed Jun 17, 2009 9:59 am
by delveart
I have been looking for a way to get one of two form fields to be valid. I have a cell phone field and a home phone field and need for at least one of them to be filled in. In all the research that I have done, it seems this is possible in Javascript but I could not find any clear answers. Here is the current validation code I am using which requires both fields to be filled in.
Code: Select all
if (empty($firstname) || empty($lastname) || empty($birthday) || empty($country) || empty($mailingaddress) || empty($city) || empty($state) || empty($zip) || empty($homephone) || empty($cellphone) || empty($option) || empty($email)) { die("Please fill in all required fields.");}
I was hoping there would be a little snippit of code that I could put in there somewhere to get the cell phone and home phone fields to communicate validation wise, but something tells me that it is not that simple. Any Help would be appreciated.
Re: Form Validation
Posted: Wed Jun 17, 2009 10:09 am
by akuji36
You could use php isset function , but wouldn't regex (using regular expressions)
be better? You could make sure field is not empty and filled with numbers only.
isset example:follow this link ---
http://www.tizag.com/phpT/phpsessions.php
and
regexpressions example:
http://blog.themeforest.net/screencasts ... pressions/
Rod

Re: Form Validation
Posted: Wed Jun 17, 2009 10:27 am
by delveart
That is a tad over my head. What I think you are saying is that the isset function can write to any field left empty. For examle, if the user does not fill out one of the phone fields, In the validation email or in a .csv file I can make it say none?
Re: Form Validation
Posted: Wed Jun 17, 2009 8:40 pm
by califdon
delveart wrote:I have been looking for a way to get one of two form fields to be valid. I have a cell phone field and a home phone field and need for at least one of them to be filled in. In all the research that I have done, it seems this is possible in Javascript but I could not find any clear answers. Here is the current validation code I am using which requires both fields to be filled in.
Code: Select all
if (empty($firstname) || empty($lastname) || empty($birthday) || empty($country) || empty($mailingaddress) || empty($city) || empty($state) || empty($zip) || empty($homephone) || empty($cellphone) || empty($option) || empty($email)) { die("Please fill in all required fields.");}
I was hoping there would be a little snippit of code that I could put in there somewhere to get the cell phone and home phone fields to communicate validation wise, but something tells me that it is not that simple. Any Help would be appreciated.
Just think about it. The above code is saying "if the firstname field is empty OR if the lastname field is empty OR if the birthday field is empty OR..." and so on. Now I believe you are saying that it's OK for either the homephone or cellphone field to be empty IF the other one isn't, right? So all you have to do is use some parentheses to combine the boolean logic, like this (and I can think of several alternatives):
Code: Select all
if (empty($firstname) || empty($lastname) || empty($birthday) || empty($country) || empty($mailingaddress) || empty($city) || empty($state) || empty($zip) || [color=#FF0000](empty($homephone) && empty($cellphone))[/color] || empty($option) || empty($email)) { die("Please fill in all required fields.");}
so it's saying "... OR
(if the homephone field is empty AND the cellphone field is empty) ..."