Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: validation

  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question validation

    Hi-

    I don't know much of PHP so I need help with validating 2 text fields in a contact form on my website. Below is the PHP code I currently have (it just sends the results of the form to my email) but I need to know how to validate the "name" and "email" fields. If the user doesn't enter anything then it should bring up an alert preventing them from submitting until they enter a value (and for the "email" field, if it can be checked as valid email ID format then that would be even better). Here is the code... let me know what to add. Thanks!

    PHP Code:
    <?php
      $email 
    $_REQUEST['email'] ;
      
    $message $_REQUEST['comments'] ;
      
    $name $_REQUEST['name'] ;

      
    mail"p_sha85@yahoo.com""Feedback from: $name",
        
    $message"From: $email" );
      
    header"Location: http://www.poojasdesigns.com/thankyou.html" );
    ?>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    to test, whether the a field has been filled either use the empty() or strlen() function.

    testing for a valid email format requires testing against a RegEx (preg_match()). there are quite a lot of such RegExes on the net, you should be able to find a suitable one through google.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Can you show me exactly how to code this? As I said, i don't know PHP and whatever I had done in this code so far was taken from a tutorial website. I have searched for validating, etc on Google but because I don't know PHP, whatever I had put in from those tutorials didn't work. That's why I need someone in this forum to show me exactly what I need to do based on the code I already have.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Email can be validated using the new filter_var functions:
    PHP Code:
    <?php 
      $email 
    $_REQUEST['email'] ; 
      
    $message $_REQUEST['comments'] ; 
      
    $name $_REQUEST['name'] ; 
      if (!
    filter_var($emailFILTER_VALIDATE_EMAIL))
      {
          die(
    'Invalid email address');
      }

      
    mail"p_sha85@yahoo.com""Feedback from: $name"
        
    $message"From: $email" ); 
      
    header"Location: http://www.poojasdesigns.com/thankyou.html" ); 
    ?>
    You also need to validate the $message, but thats dependant on what you want to allow. You can use strip_tags to remove any provided html code, or you can use htmlspecialchars / htmlentities to translate them into readable but unusable format. Filter_var can also do this, by using a string santiation to remove all high and low (32 >= character <= 127).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    psha85 (06-16-2010)

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Great, thanks so much for showing me how to do this! And how can I validate the name field to ensure that some value was entered?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Same ways as any:
    PHP Code:
    <?php  
      $email 
    $_REQUEST['email'] ;  
      
    $message trim($_REQUEST['comments']);  
      
    $name trim($_REQUEST['name']);

      
    // Fix up the name and message.  not worried about the email, any badness will be caught anyway
      
    $message strip_tags($message);
      
    $name strip_tags($name);

      if (!
    filter_var($emailFILTER_VALIDATE_EMAIL)) 
      { 
          die(
    'Invalid email address'); 
      } 
      if (empty(
    $name))
      {
            die(
    'Name cannot be empty.');
      }
      if (empty(
    $message))
      {
          die(
    'Message cannot be empty');
      }
      
      
    mail"p_sha85@yahoo.com""Feedback from: $name",  
        
    $message"From: $email" );  
      
    header"Location: http://www.poojasdesigns.com/thankyou.html" );  
    ?>
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    psha85 (06-16-2010)

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    works perfectly.. thanks!


  •  

    Posting Permissions

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