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
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts

    Form Validation Help

    Im trying to make a script that will only allow use full information in a form on my website... Here is the code i have so far...

    PHP Code:
    <?php
      
    if(isset($_POST['submit']))
          {
              
    $name $_GET['name'];
              
    $email $_POST['email'];
              
    $mobile $_POST['mobile'];
              
    $comments $_POST['comments'];
     
              if(empty(
    $errors))
              {
                
    $to "info@eventpromotion.ie";
                
    $subject "Event Promotion Enquiry!";
                
    $body 
                 
    "First Name: " $_POST['name'] .
                 
    "\nEmail: " $_POST['email'] .  
                   
    "\nMobile: " $_POST['mobile'] . 
                 
    "\nMessage: " $_POST['comments'];
               if (
    mail($to$subject$body)) {
                echo(
    "<p>Thanks for submitting your enquiry.</p>");
                }
                else
                {
                echo(
    "<p>Message delivery failed.</p>");
                }
              }
              else
              {
              echo 
    "<p>".$error."</p>";
              }    
          }                   
    ?>
        <form id="form" method="post" action="testing.php"> 
                  <p> 
                    <label>Name</label><br /> 
                    <input type="text" name="name" id="firstName" />      
                </p> 
                  <p> 
                    <label>Email:</label><br /> 
                    <input type="text" name="email" id="email" /> 
                    
                </p>               
                <p> 
                    <label>Mobile:</label><br /> 
                    <input type="text" name="mobile" id="mobile" /> 
                    
                </p>                                  
                <p> 
                    <label>Comments:</label> <br />
                    <textarea name="comments" cols="30" rows="3"></textarea>    
                </p> 
                <p>    <input type="submit" name="submit" value="Submit"  /></p> 
      </form>
    I only want the form to be processed if the form fields are correctly filled out..

    for name i want something like the input to be two words of a minimum of 3 charaters each maybe.. and if the user has not put this i want the script to display an error above the form saying something like "Please insert your full name with a space between your first and last name"

    for the email field i would like a correctly formatted email address to be used and again if there is an error i would like the message to say something like "Please enter your correct email address"

    For the phone field i would like the user to only enter a ten digit number

    and for the comments maybe have a minimum of 20 chars..

    Can somebody help me with this its bugging me all morning and i cant figure out the logic in it..

    Thanks for your help...

  • #2
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by kevinkhan View Post
    for name i want something like the input to be two words of a minimum of 3 charaters each maybe.. and if the user has not put this i want the script to display an error above the form saying something like "Please insert your full name with a space between your first and last name"

    for the email field i would like a correctly formatted email address to be used and again if there is an error i would like the message to say something like "Please enter your correct email address"

    For the phone field i would like the user to only enter a ten digit number

    and for the comments maybe have a minimum of 20 chars..

    Can somebody help me with this its bugging me all morning and i cant figure out the logic in it..

    Thanks for your help...
    You need to investigate regular expression and then write a few validation functions to specify your rules. For example (not fully tested but functional):

    PHP Code:
    $strEmail 'test@test.com';
    $strName 'blah blah';
    $strComments 'blah blah blah blah blah';
    $strPhone '1234567890';

    $arrErrors = array();
    if(!
    Validate_Email($strEmail)) {    $arrErrors[] = 'Bad email';    }
    if(!
    Validate_Regex($strName,'/^\w{3,}\s+\w{3,}$/')) {    $arrErrors[] = 'Bad name';    }
    if(!
    Validate_Length($strComments,20,1000)) {    $arrErrors[] = 'Bad comment';    }
    if(!
    Validate_Regex($strPhone,'/^\d{10}$/')) {    $arrErrors[] = 'Bad phone';    }
    if(
    count($arrErrors)) {
        echo(
    'errors: <ul><li>'.implode('<li></li>',$arrErrors).'</li></ul>');
    } else {
        echo(
    'ok');
    }
    function 
    Validate_Length($strValue,$intMin,$intMax) {
       return((
    strlen($strValue) >= $intMin) and (strlen($strValue) <= $intMax));
    }
    function 
    Validate_Email($strValue) {
       
    $strPattern '/^[a-z0-9&\'\.\-_\+!#$%*\/=?^`{\|}~]+@[a-z0-9\-]+\.(?:[a-z0-9\-]+\.)*?[a-z]{2,4}$/i';
       return(
    preg_match($strPattern,$strValue));
    }
    function 
    Validate_Regex($strValue,$strPattern) {
       return(
    preg_match($strPattern,$strValue));

    HTH,

    Dai

  • Users who have thanked DaiWelsh for this post:

    kevinkhan (02-17-2010)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post

    easy way

    hi

    there is a very easy way of doing this. but you will have to have dreamweaver.

    dreamweaver have some very good validation functions like email, inters, ceditcard numbers, zip code and simple things like mini character and max es.t

    dreamweaver validation works using javascript. if you want to look into it furth google spry validation.

    if you don't have dreamweaver have a look here:

    http://www.w3schools.com/js/js_form_validation.asp
    or have a google on javascript validation

    hope this help

    p.s

    sorry if you wanted the validation in php but i find java script easier to work with when doing validations.

  • Users who have thanked phpcodelearner for this post:

    kevinkhan (02-17-2010)

  • #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
    Quote Originally Posted by phpcodelearner View Post
    hi

    there is a very easy way of doing this. but you will have to have dreamweaver.

    dreamweaver have some very good validation functions like email, inters, ceditcard numbers, zip code and simple things like mini character and max es.t

    dreamweaver validation works using javascript. if you want to look into it furth google spry validation.

    if you don't have dreamweaver have a look here:

    http://www.w3schools.com/js/js_form_validation.asp
    or have a google on javascript validation

    hope this help

    p.s

    sorry if you wanted the validation in php but i find java script easier to work with when doing validations.
    Javascript is not an acceptable solution to form validation. It can be used to indicate problems before hitting processing, but should never be used in place of server validation. Ever.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

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

    kevinkhan (02-17-2010)

  • #5
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    THanks for your help guys.. I think i have it

  • #6
    Regular Coder
    Join Date
    Jun 2009
    Posts
    351
    Thanks
    75
    Thanked 0 Times in 0 Posts
    Thanks for your help guys. I came up with this code in the end

    PHP Code:
    <?php



      
    if(isset($_POST['submit']))
          {
              
    $firstName $_POST['firstName'];
              
    $lastName $_POST['lastName'];
              
    $email $_POST['email'];
              
    $mobile $_POST['mobile'];
              
    $comments $_POST['comments'];
             
              
    $errors = array();
              
            function 
    display_errors($error_array)
              {
              echo 
    "<p class=\"errors\">";
              
              foreach(
    $error_array as $error)
                {
                echo 
    $error "<br />";
                }
                echo 
    "</p>";
              }  
              
             function 
    validateNames($names
            {
            return(
    strlen($names) < 3);
            } 
            
            function 
    validateEmail($strValue
            {
               
    $strPattern '/([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})/sim';
               return(
    preg_match($strPattern,$strValue));
            } 
            
            function 
    validateMobile($strValue
            {
               
    $strPattern '/^\d{10}$/';
               return(
    preg_match($strPattern,$strValue));
            } 
            
            function 
    validateComments($comments
            {
            return(
    strlen($comments) < 10);
            } 
            
            if(
    validateNames($firstName)) 
            {    
            
    $errors[] = 'Please Enter Your First Name';    
            }
            
             if(
    validateNames($lastName)) 
            {    
            
    $errors[] = 'Please Enter Your Second Name';    
            }
            
              
            if(!
    validateEmail($email)) 
            {    
            
    $errors[] = 'Please Enter Your Correct Email';    
            }
            
            if(!
    validateMobile($mobile)) 
            {    
            
    $errors[] = 'Please Enter Your Correct Mobile Number';    
            }
            
            if(
    validateComments($comments)) 
            {    
            
    $errors[] = 'Please Enter A Comment More Than 10 Characters';    
            }


              if(empty(
    $errors))
              {
                
    $to "info@eventpromotion.ie";
                
    $subject "Event Promotion Enquiry!";
                
    $body 
                 
    "First Name: " $_POST['firstName'] .
                 
    "\nLast Name: " $_POST['lastName'] .
                 
    "\nEmail: " $_POST['email'] .  
                   
    "\nMobile: " $_POST['mobile'] . 
                 
    "\nMessage: " $_POST['comments'];
                  
    $headers "From: "$firstName ." "$lastName " <" $email ">\r\n";


              if (
    mail($to$subject$body$headers)) {
                echo(
    "<p>Thanks for submitting your enquiry.</p>");
                }
                else
                {
                echo(
    "<p>Message delivery failed.</p>");
                }
              }
              else
              {
              
    //echo "error";
             
    display_errors($errors);
              }    
          }                   
    ?>
        <form id="form" method="post" action="testing.php"> 
                  <p> 
                    <label>First Name</label><br /> 
                    <input type="text" name="firstName" value="<?php if(isset($firstName)){echo $firstName;} ?>" />      
                </p> 
                <p> 
                    <label>Last Name</label><br /> 
                    <input type="text" name="lastName" value="<?php if(isset($lastName)){echo $lastName;} ?>" />      
                </p> 
                  <p> 
                    <label>Email:</label><br /> 
                    <input type="text" name="email" value="<?php if(isset($email)){echo $email;} ?>" /> 
                    
                </p>               
                <p> 
                    <label>Mobile:</label><br /> 
                    <input type="text" name="mobile" value="<?php if(isset($mobile)){echo $mobile;} ?>" /> 
                    
                </p>                                  
                <p> 
                    <label>Comments:</label> <br />
                    <textarea name="comments" cols="30" rows="3" ><?php if(isset($comments)){echo $comments;} ?></textarea>    
                </p> 
                <p>    <input type="submit" name="submit" value="Submit"  /></p> 
      </form>
    if there are more than one error how do i limit to displaying only one error at a time...

    do i need a different loop or how would i go about doing it does anyone know?

  • #7
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Netscape

    Quote Originally Posted by kevinkhan View Post

    if there are more than one error how do i limit to displaying only one error at a time...

    do i need a different loop or how would i go about doing it does anyone know?
    My first though is why? For me being told about form validation errors one at a time drives me potty?

    However if that is just me and you really want to do it just change this function

    PHP Code:
    function display_errors($error_array)
    {
       echo 
    "<p class=\"errors\">";
       echo 
    $error_array[0];
       echo 
    "</p>";



  •  

    Posting Permissions

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