Jump to content

Form Validation


richnb1974

Recommended Posts

Hi Guys, Im a bit of a novice to Php but somehow I've managed to get myself a form together for customers to book online. My problem is that i need to validate all the input's of my form. To give you some idea of what i mean. The $name should only contain letters and be no longer than 25 characters. The $address should only contain letters & numbers and be no longer than 40 characters. The $district and $town should only contain letters and be no longer than 30 characters each. The $postcode should only contain letters and Numbers and be no longer than 8 characters, it should start and end with a letter if thats possible. The $phone should only contain numbers and be no longer than 11 characters. And the email, well im lost with how that should be set out because of all the different options you can have.

Anyway, this is my code:

 

<?php $to = 'myemail.com' ;

                  $from = $_REQUEST['Email'] ;

                  $name = $_REQUEST['Name'] ;

                  $phone = $_REQUEST['Phone'];

                  $headers = "From: $from";

                  $subject = "Booking Request";

                  $fields = array();

                  $fields{"Name"} = "Name";

                  $fields{"Address"} = "Address";

                  $fields{"District"} = "District";

                  $fields{"Town"} = "Town";

                  $fields{"Postcode"} = "Postcode";

                  $fields{"Phone"} = "Phone";

                  $fields{"Email"} = "Email";

                  $fields{"list"} = "Mailing List";

                  $fields{"Message"} = "Message";

                  $body = "We have received the following information:\n\n"; foreach($fields as $a => $b)

                  {        $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

 

 

 

                        if ($name == '') {print "You have not entered a name, please go back and try again";}

                        elseif ($phone == '') {print "You have not entered a Phone number, please go back and try again";}

 

 

                        else { $send = mail($to, $subject, $body, $headers);

 

                        if($send) {header( "Location: http://www.(not applicable).com" );}

                        else {print "We encountered an error sending your mail, please notify [email protected]";  } } ?>

 

Any help is greatly apprecaited.

 

Kind regards

 

Rich

 

Link to comment
https://forums.phpfreaks.com/topic/209878-form-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.