1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form Validation

Discussion in 'PHP' started by johneva, Mar 26, 2010.

  1. #1
    Hi guys

    I got a form and it works it send the data needed to the email address required, but I am having trouble getting the validation part of it working as needed.

    Its allowing no entery for phone number, I would also like some advise to prevent people entering 00000 000000 so people have to enter what is more likely to be a real phone number.

    Any help/advise on this would be great cheers.

                  <?php
    if(isset($_POST['email'])) {
    	
    	// EDIT THE 2 LINES BELOW AS REQUIRED
    	$email_to = "my email here";
    	$email_subject = "email subject here";
    	
    	
    	function died($error) {
    		// your error code can go here
    		echo "<br /><p>We are very sorry, but there were errors found with the form you submitted. ";
    		echo "These errors appear below.</p>";
    		echo "<p> ".$error."</p>";
    		echo "<p>Please go back and fix these errors.</p>";
    		echo "<p><a href='#' onClick='history.go(-1)'><img src='img/green-arrow.png' alt='Green Back Arrow' /></a></p>";
    		die();
    	}
    	
    	// validation expected data exists
    	if(!isset($_POST['name']) ||
                    !isset($_POST['phone']) ||
                    !isset($_POST['mobile']) ||
                    !isset($_POST['email']) ||
                    !isset($_POST['vehicle']) ||
    		!isset($_POST['term'])) {
    		died('We are sorry, but there appears to be a problem with the form your submitted.');		
    	}
    	
    	$name = $_POST['name']; // required
            $company_name = $_POST['company_name']; // not required
            $telephone = $_POST['phone']; // required        
            $mobile = $_POST['mobile']; // required
    	$email_from = $_POST['email']; // required
            $vehicle = $_POST['vehicle']; // required
            $term = $_POST['term']; // required
    
            
    	$error_message = "";
    	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
      if(!eregi($email_exp,$email_from)) {
      	$error_message .= '* The Email Address you entered does not appear to be valid.<br />';
      }
    	$string_exp = "^[a-z .'-]+$";
      if(!eregi($string_exp,$name)) {
      	$error_message .= '* The Name you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
      	died($error_message);
      }
    	$email_message = "Contact Details.\n\n";
    	
    	function clean_string($string) {
    	  $bad = array("content-type","bcc:","to:","cc:","href");
    	  return str_replace($bad,"",$string);
    	}
    	
    	$email_message .= "Name: ".clean_string($name)."\n";
    	$email_message .= "Company Name: ".clean_string($company_name)."\n";
    	$email_message .= "Email: ".clean_string($email_from)."\n";
    	$email_message .= "Telephone: ".clean_string($telephone)."\n";
    	$email_message .= "Mobile: ".clean_string($mobile)."\n";
         	$email_message .= "Vehicle: ".clean_string($vehicle)."\n";
         	$email_message .= "Term: ".clean_string($term)." Months\n";
    	
    	
    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);  
    ?>
    
    <!-- include your own success html here -->
    
              <p>&nbsp;</p>
              <h2>Thank you for contacting us for your quote.<br /> We will be in touch with you very soon.</h2>
    
    
    <?
    }
    ?>
    PHP:
     
    johneva, Mar 26, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    There's bound to be a regex for it but be careful if your site is international... I get fed up with sites that don't accept my phone number unless I add the country code and area code to make it resemble an american phone number.
     
    sarahk, Mar 26, 2010 IP
  3. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #3
    Ah good point I did not think of but we are only UK service anyway.
     
    johneva, Mar 26, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    
    //validate UK mobile number... - such as 07707510125
    preg_match('~^([0-9]{11})$~', trim($var));
    PHP:
     
    danx10, Mar 26, 2010 IP
  5. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #5
    Awsome I have it working so both numbers have to be valid, how would I code it to say one or the other has to be valid?

    So if they put in their mainland number they dont have to put in mobile but otherwise they will have to enter mobile numer and vise versa.
     
    johneva, Mar 26, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    @johneva


    <?php
      function died($error)
      {
          // your error code can go here
          echo "<br /><p>We are very sorry, but there were errors found with the form you submitted. ";
          echo "These errors appear below.</p>";
          echo "<p> " . $error . "</p>";
          echo "<p>Please go back and fix these errors.</p>";
          echo "<p><a href='#' onClick='history.go(-1)'><img src='img/green-arrow.png' alt='Green Back Arrow' /></a></p>";
          die();
      }
      
      
      function clean_string($string)
      {
          $bad = array("content-type", "bcc:", "to:", "cc:", "href");
          return str_replace($bad, "", $string);
      }
      
      if (isset($_POST['email'])) {
          // EDIT THE 2 LINES BELOW AS REQUIRED
          $email_to = "my email here";
          $email_subject = "email subject here";
          
          
          
          
          // validation expected data exists
          if (!isset($_POST['name']) || !isset($_POST['phone']) || !isset($_POST['mobile']) || !isset($_POST['email']) || !isset($_POST['vehicle']) || !isset($_POST['term'])) {
              died('We are sorry, but there appears to be a problem with the form your submitted.');
          }
          
          
          //sanitization hack..
          $_POST = array_map("strip_tags", $_POST);
          // required
          $name = $_POST['name'];
          // not required
          $company_name = $_POST['company_name'];
          // required        
          $telephone = $_POST['phone'];
          // required
          $mobile = $_POST['mobile'];
          // required
          $email_from = $_POST['email'];
          // required
          $vehicle = $_POST['vehicle'];
          // required
          $term = $_POST['term'];
          
          
          $error_message = "";
          $email_exp = "~^[A-Z0-9\._%\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,4}$~i";
          if (!preg_match($email_exp, $email_from)) {
              $error_message .= '* The Email Address you entered does not appear to be valid.<br />';
          }
          $string_exp = "~^[a-z .'-]+$~";
          if (!preg_match($string_exp, $name)) {
              $error_message .= '* The Name you entered does not appear to be valid.<br />';
          }
          
          $phone_exp = "~^([0-9]{11})$~";
          
          if(!preg_match($phone_exp , $mobile) && !preg_match($phone_exp, $telephone)){
              $error_message .= '* You have not entered a mobile and/or telephone number.<br />';
          }
          
          if (strlen($error_message) > 0) {
              died($error_message);
          }
          
          $email_message = "Contact Details.\n\n";
          
          
          $email_message .= "Name: " . clean_string($name) . "\n";
          $email_message .= "Company Name: " . clean_string($company_name) . "\n";
          $email_message .= "Email: " . clean_string($email_from) . "\n";
          $email_message .= "Telephone: " . clean_string($telephone) . "\n";
          $email_message .= "Mobile: " . clean_string($mobile) . "\n";
          $email_message .= "Vehicle: " . clean_string($vehicle) . "\n";
          $email_message .= "Term: " . clean_string($term) . " Months\n";
          
          
          // create email headers
          $headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
          @mail($email_to, $email_subject, $email_message, $headers);
    ?>
    
    <!-- include your own success html here -->
    
              <p>&nbsp;</p>
              <h2>Thank you for contacting us for your quote.<br /> We will be in touch with you very soon.</h2>
    
    
    <?php
      }
    ?>
    PHP:
    Also cleaned up your code alittle :p
     
    danx10, Mar 26, 2010 IP
    johneva likes this.