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 webmaster@YourCompany.com";  } } ?>

 

Any help is greatly apprecaited.

 

Kind regards

 

Rich

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.