Log in

View Full Version : PHP Form Validation



cdmoody
11-20-2009, 01:09 AM
Hi,
I'm new to PHP and I'm struggling with a project that requires form validation. The requirements are all fields in the form are required, error message should appear if fields are not completed or completed incorrectly. Phone and email should be in proper format. A confirmation page should appear, thanking the visitor and the page will include all information just entered.

Entered data is written to a file and an email is sent to myself and the visitor. The visitor's email should confirm the info and request a reply.

I've searched tons of resources and I have no idea what I'm doing. I've created the form already.


Here's the form:


<form id="contact" name="contact" method="post" class="form">

<p>
<label for="fname">First Name</label>
&nbsp;
<input name="fname" type="text" id="fname" size="50" />
</p>

<label for="lname">Last Name</label>
&nbsp;
<input name="lname" type="text" id="lname" size="50" />

<p>
<label for="title">Job Title</label>
&nbsp;
&nbsp;
<input name="title" type="text" id="title" size="50" />
</p>
<p>
<label for="company">Company</label>
<input name="company" type="text" id="company" size="50" />
</p>
<p>
<label for="address">Mailing Address</label>

<input type="text" name="address" id="address" size="50" /> <br />
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;
&nbsp;&nbsp;



<input type="text" name="address" id="address" size="50" />
</p>
<p>
<label for="city">City</label>
<input type="text" name="city" id="city" size="25" />
<label for="zip">Zip Code</label>
<input type="text" name="zip" id="zip" size="10" />
<label for="state">State</label>
<input type="text" name="state" id="state" size="2" />
</p>

<p>
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone" size="3" />
<input type="text" name="phone" id="phone" size="3" />
<input type="text" name="phone" id="phone" size="4" />
<label for="fax">Fax Number</label>
<input type="text" name="fax" id="fax" size="3" />
<input type="text" name="fax" id="fax" size="3" />
<input type="text" name="fax" id="fax" size="4" />
</p>

<p>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" size="50" />
<p> Enter any additional comments: <br />
<textarea name="comment" cols="40" rows="5"></textarea>
</p>
<p>
<input type="submit" name="submit" value="Submit"/>
<input name="reset" type="reset" value="Reset" />
</p>
</form>

traq
11-20-2009, 03:06 AM
plug the value into preg_match:

if(preg_match('/putPatternHere/', $valueToTest)){ echo 'good value.'; }else{ echo 'bad value!'; }
to check if something is not empty: .+ (means 1 or more of any character)
lotsa ways to check email addresses (http://regexlib.com/DisplayPatterns.aspx).
phone numbers... well, what do you mean by "proper format"?
...1-234-567-8901
...1(234)567-8901
...1.234.567.8901
...(234)567-8901
...567-8901
many more possibilities, none of which are "wrong", exactly. And that's not counting country codes, etc.

james438
11-20-2009, 03:16 AM
<?php
$test='abc@domain.info';
if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})$/',
$test)) {echo "YES";}
else {echo "NO";}
?>

should check most emails. traq is right in that there is no "proper format", but some are better than others and there is none yet that can check all email types, but the above should match most and is simple.

cdmoody
11-20-2009, 11:51 AM
Sorry I didn't explain further. By proper format I am required to ensure that the numbers are 234-567-8901 and the email should be xxx@xxxxxxx.xxx

If I use what James has provided that will check email correct? And then for each other field in my form I should use the code that traq provided, correct? Now my question is how do I get it to show a confirmation page, write the data to a text file, send an email to the visitor to confirm the info and then send an email to myself?

Also, should this code be placed in an external file? Sorry for all the questions.

By the way, thanks for you help!

james438
11-21-2009, 03:20 AM
Just answering some of your questions, my regex should check most emails. What traq posted is the basic framework for checking phone numbers. Here is an example of one that will check phone numbers. In particular it checks US phone numbers and puts it into the format you mentioned, if it is a US phone number that is. I got it from php.net and you can read more about it here (http://www.php.net/manual/en/function.preg-match.php#90503).


Detect and reformat phone numbers:

<?php
$phoneNumber="337-333-3443";
$regex = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})'
.'(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})'
.'[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';

if (preg_match($regex, $phoneNumber))

{$formatted = preg_replace($regex, '$1-$2-$3', $phoneNumber);
echo "$formatted";}
else echo "NOT A MATCH";
?>

You can write the info into a text file, but depending on your needs it is usually better to store the data into a database. Sending confirmation emails is not something I know how to do.

What do you mean by a confirmation page? Do you mean that you want to submit the user submitted data to another page and if the submitted data is not valid then go back to the previous page? I am also curious what you mean by an external file. Do you mean like an 'include' file? That might not be a bad idea, but not necessary. It is mostly a way to make managing code easier and quicker.

Well, at least we now have a regex to detect emails and another for US phone numbers :)

cdmoody
11-21-2009, 03:58 AM
For the external file I was talking about an include page. I thought I had to put it in another file then call the file or something like that. If you don't use the include page then do you just paste the code directly into the html document? Is there a certain area that the code goes?

The assignment instructions are asking for a confirmation page. I either need the confirmation page to be either in email format that is sent to the visitor or a page that appears after the form is submitted.

james438
11-21-2009, 09:12 AM
You can post php code anywhere into an html document provided you begin your php statements with <? or <?php and end your php statements with ?>. You should also save your documents with a .php extension. The extensions determine in certain cases how the file will be used. I seem to remember a file with an html extension executing php scripts, but I suspect that in order to get that to work you need to mess with the php.ini file at the very least or have the use of a dedicated server, which is more advanced and expensive than I care to get.

Here is an example:

<?php $genre="data"; ?>
<input type="text" name="genre" cols=45
value="<?php print $genre;?>">

In this case I suggest creating your html form and submitting the data to another page using $_POST where the data will be examined and processed if need be. Then if the data is bad you can run a short javascript to redirect the person back to the html form page to try again, finish, and/or edit their data.

prasanthmj
11-21-2009, 10:05 AM
The following pages might be helpful:
PHP form validation script (http://www.html-form-guide.com/php-form/php-form-validation.html)
and
PHP form tutorial: first steps (http://www.html-form-guide.com/php-form/php-form-tutorial.html)