Jump to content

Simple form validation


andrew_biggart

Recommended Posts

I am trying to add some simple validation to my form. I only wanter users to be able to enter 11 numbers into the number field or else it will say 11 digit numbers only. A validation to only allow users to input numbers anything else and it brings up an error. and finally i want to check the email string to make sure it has an @ and a (.) .

 

How would i go about adding these validation techniques to this code?

 

 

			<?php
                    if ($_SERVER['REQUEST_METHOD']=="POST"){
                                                
                    // Recipent Email
                    $to="[email protected]";
                                                        
                    $subject="Reply to Peppermint Invitation";
                    
                    $title = $_POST['title'];
                    $name = stripslashes($_POST['name']);
                    $add1 = stripslashes($_POST['add1']);
                    $add2 = stripslashes($_POST['add2']);
                    $add3 = stripslashes($_POST['add3']);
                    $add4 = stripslashes($_POST['add4']);
                    $postcode = stripslashes($_POST['postcode']);
                    $number = stripslashes($_POST['number']);
                    $email = stripslashes($_POST['email']);
                    $time = $_POST['time'];
                    
                                                        
                    $from = ($_POST['name']) . "<".stripslashes($_POST['email']).">";

				$subject = ($_POST['name']) . "<".stripslashes($_POST['email']).">";
                                                        
                    // Email Message
                    $message = "Name : $title $name. Address : $add1, $add2, $add3, $add4, $postcode. Phone Number : $number. Email : $email. Appointment Time : $time";


                                                        
                                                        
                    // Validation Begins 

                    // Add Erros To Array
                    $errors = array();
                     
                    // Check Form
                    if (!$_POST['title'])
                    $errors[] = "Title Required";
        
                    if (!$_POST['name'])
                    $errors[] = "Name Required";
                    
                    if (!$_POST['add1'])
                    $errors[] = "Address Required";

				if (!$_POST['add4'])
                    $errors[] = "City Required";
                    
                    if (!$_POST['postcode'])
                    $errors[] = "Postcode Required";
                    
                    if (!$_POST['number'])
                    $errors[] = "Number Required";

				if(!$_POST['number'] == 11)
				$errors[] = "Full Number Required";
                   
                    if (!$_POST['email'])
                    $errors[] = "Email Required";
                    
                    if (!$_POST['time'])
                    $errors[] = "Time Required";
                    
                    
                    // Display Errors
                    
                    if (count($errors)>0)
                    
                    {
                    echo"<h1 class='fail'>";
                    foreach($errors as $err)
                    echo "$err.\n";
                    echo"</h1>";


			echo "


        <form method='post' action='#'>
        	<table cellpadding='0' cellspacing='0' border='0'>
            	<tr>
                    <td class='form_results' colspan='2'>               
                    </td>
                </tr>
            	<tr>
               	  	<td class='form_left' valign='top'>
                  	  <table width='200' border='0' cellspacing='0' cellpadding='0'>
                          <tr>
                            <td class='title_left' valign='top'>Title</td>
                          </tr>
                          <tr>
                            <td class='sname_left' valign='top'>Name</td>
                          </tr>
                          <tr>
                            <td class='add_left' valign='top'>Address</td>
                          </tr>
                          <tr>
                            <td class='post_left' valign='top'>Postcode</td>
                          </tr>
                          <tr>
                            <td class='number_left' valign='top'>Phone Number</td>
                          </tr>
                          <tr>
                            <td class='email_left' valign='top'>Email Address</td>
                          </tr>
                          <tr>
                            <td class='time_left' valign='top'>Appointment Time</td>
                          </tr>
                        </table>
                  </td>
                  <td class='form_right' valign='top'>
                        <table width='300' border='0' cellspacing='0' cellpadding='0'>
                          <tr>
                            <td class='right_spacer'>
                            	<select name='title' class='select'>
                                  <option>Mr</option>
                                  <option>Mrs</option>
                                  <option>Ms</option>
                                  <option>Miss</option>
                                </select>
                            </td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='name' class='field' value='$name' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add1' class='field' value='$add1' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add2' class='field' value='$add2' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add3' class='field' value='$add3' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add4' class='field' value='$add4' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='postcode' class='field' value='$postcode' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='number' class='field' value='$number' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='email' class='field' value='$email' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'>
                            	<select name='time' class='select'>
                                  <option>5.30 pm</option>
                                  <option>6.30 pm</option>
                                  <option>7.30 pm</option>
                                </select>
                            </td>
                          </tr>
                        </table>
                  </td>
                </tr>
                <tr>
                  <td class='form_buttom' colspan='2'>
                   <input name='submit' type='submit' value='Submit' class='submit_button' />
                  </td>
                </tr>
             </table>
             </form>				



				 ";
                    }
                    
                    else {  



				                  
                                                        
                                                        
                    // Build message headers
                    $headers = "From: $from\r\n" .
                    "MIME-Version: 1.0\r\n" .
                    "Content-Type: multipart/mixed;\r\n" .
                    " boundary=\"{$mime_boundary}\"";
                                                        
                    
                                                        
                    // Send message
                        if (@mail($to, $subject, $message, $headers))
                        echo "<h1 class='success'>Your message has been sent.</h1>";
                        
                        else
                        echo "<h1 class='fail'>Your message was not sent at this time.</h1>";
                        }
                     }
                    else {
			echo "


        <form method='post' action='#'>
        	<table cellpadding='0' cellspacing='0' border='0'>
            	<tr>
                    <td class='form_results' colspan='2'>               
                    </td>
                </tr>
            	<tr>
               	  	<td class='form_left' valign='top'>
                  	  <table width='200' border='0' cellspacing='0' cellpadding='0'>
                          <tr>
                            <td class='title_left' valign='top'>Title</td>
                          </tr>
                          <tr>
                            <td class='sname_left' valign='top'>Name</td>
                          </tr>
                          <tr>
                            <td class='add_left' valign='top'>Address</td>
                          </tr>
                          <tr>
                            <td class='post_left' valign='top'>Postcode</td>
                          </tr>
                          <tr>
                            <td class='number_left' valign='top'>Phone Number</td>
                          </tr>
                          <tr>
                            <td class='email_left' valign='top'>Email Address</td>
                          </tr>
                          <tr>
                            <td class='time_left' valign='top'>Appointment Time</td>
                          </tr>
                        </table>
                  </td>
                  <td class='form_right' valign='top'>
                        <table width='300' border='0' cellspacing='0' cellpadding='0'>
                          <tr>
                            <td class='right_spacer'>
                            	<select name='title' class='select'>
                                  <option>Mr</option>
                                  <option>Mrs</option>
                                  <option>Ms</option>
                                  <option>Miss</option>
                                </select>
                            </td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='name' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add1' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add2' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add3' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='add4' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='postcode' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='number' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'><input type='text' name='email' class='field' /></td>
                          </tr>
                          <tr>
                            <td class='right_spacer'>
                            	<select name='time' class='select'>
                                  <option>5.30 pm</option>
                                  <option>6.30 pm</option>
                                  <option>7.30 pm</option>
                                </select>
                            </td>
                          </tr>
                        </table>
                  </td>
                </tr>
                <tr>
                  <td class='form_buttom' colspan='2'>
                   <input name='submit' type='submit' value='Submit' class='submit_button' />
                  </td>
                </tr>
             </table>
             </form>				



				 ";						
		 } 
                    ?>	

 

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

this function will return a string stripped of all but numbers. then you can count the numbers:

 

 

function allNumbers($input_str) { // FASTER?return preg_replace("/[\D]/","",$input_str);}

 

 

for finding a specific character in a string, check out stristr(), or preg_match() for more power

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.