Results 1 to 2 of 2

Thread: PHP form Validation

  1. #1
    Join Date
    Mar 2009
    Posts
    42
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default PHP form Validation

    Hi all,

    I have a problem i've been tryin to workout?

    Here is the syntax:

    PHP Code:
        <?php

    // process the email
    if (array_key_exists('submit'$_POST)) {
      
    ini_set("sendmail_from""info@test.com");
      
    $to 'info@test.com'// use your own email address
      
    $subject 'Feedback from website';
      
      
    // list expected fields
      
    $expected = array('name''email''tel''from''dep');
      
    // set required fields
      
    $required = array('name''email''tel');
      
    // create empty array for any missing fields
      
    $missing = array();
      
      
    // process the $_POST variables
      
    foreach ($_POST as $key => $value) {
        
    // assign to temporary variable and strip whitespace if not an array
        
    $temp is_array($value) ? $value trim($value);
        
    // if empty and required, add to $missing array
        
    if (empty($temp) && in_array($key$required)) {
          
    array_push($missing$key);
          }
        
    // otherwise, assign to a variable of the same name as $key
        
    elseif (in_array($key$expected)) {
          ${
    $key} = $temp;
          }
        }
     
      
    // go ahead only if all required fields OK
      
    if (empty($missing)) {
        
    // build the message
        
    $message "Name: $name\n\n";
        
    $message .= "Email: $email\n\n";
        
    $message .= "Contact No.: $tel\n\n";
        
    $message .= "Arrive: $from\n\n";
        
    $message .= "Depart: $dep\n\n";


        
    // limit line length to 70 characters
        //$message = wordwrap($message, 70);
      
        // send it  
        
    $sendmail_from mail($to$subject$message);
        if (
    $sendmail_from) {
          
    // $missing is no longer needed if the email is sent, so unset it
          
    unset($missing);
          }
        }
      }
    ?>
        <?php
            
    if ($_POST && isset($missing)) {
    ?>
        <p class="main_warning">Please complete the missing item(s) indicated. Your message has not been sent.</p>
        <?php
              
    }
            elseif (
    $_POST && !$sendmail_from) {
    ?>
        <p class="main_warning">Sorry, there was a problem sending your message. Please try later.</p>
        <?php
              
    }
            elseif (
    $_POST && $sendmail_from) {
    ?>

    // form begins

        <span class="confmsg"><strong>Thank you for your enquiry. We will contact you &amp; confirm your booking.</strong></span>
        <?php ?>
        <form id="topnavform" name="topnavform" method="post" action="">
          <label id="cal"><abbr title="Enter the date when you will be arriving">From:</abbr>
            <input type="text" name="from" id="from" maxlength="10" />
            <a href="javascript:showCal('Calendar1')"><img src="img/calimg.jpg" width="17" height="17" alt="Calendar" title="Calendar" /></a></label>
          <?php
        
    if (isset($missing) && in_array('name'$missing)) { ?>
          <span class="warning">Enter your name.</span>
          <?php ?>
          <label><abbr title="Enter your name."><font color="#FF0000">*</font>Name:</abbr>
            <input type="text" name="name" id="name" maxlength="35" />
          </label>
          <label id="calb"><abbr title="Select the date when you will be departing.">To:</abbr>
            <input type="text" name="dep" id="to" maxlength="10" />
            <img src="img/calimg.jpg" width="17" height="17" alt="Calendar" title="Calendar" /> </label>
          <?php
        
    if (isset($missing) && in_array('email'$missing)) { ?>
          <span class="warning">Enter your email address.</span>
          <?php ?>
          <label><abbr title="Enter your email address."><font color="#FF0000">*</font>Email:</abbr>
            <input type="text" name="email" id="email" maxlength="40"/>
          </label>
          <?php
        
    if (isset($missing) && in_array('tel'$missing)) { ?>
          <span class="warning">Enter your contact no.</span>
          <?php ?>
          <label><abbr title="Enter your contact no."><font color="#FF0000">*</font>Tel:</abbr>
            <input type="text" name="tel" id="tel" maxlength="25" />

            <input type="submit" name="submit" id="submit" class="submit" value="Send" title="Send" />
            <input type="reset" name="reset" id="reset" class="reset" value="Reset" title="Reset" />
          </label>
        </form>
      </div>
    All working fine, but I'm trying to add a validation in the email field (@ must be present in the filed), so that visitors cannot enter bogus emails.

    You help and suggestions are much appreciated, thank you
    Last edited by john0611; 05-10-2009 at 04:59 PM. Reason: added [php] tags

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    This is one way to validate the email address:-

    PHP Code:
    if(!eregi("^[_&a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+
    (.[a-z0-9-]+)*(.[a-z]{2,4})$"
    $email)) {

    echo 
    'Invalid format of email address';


    Replace '$email' with the field name you are using, if it fails then return to the "Enter Email Address" input.

    However, this a request that has been made many times on this forum so a search should turn up many other examples for you.

  3. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    john0611 (05-10-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •