SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast weekyd's Avatar
    Join Date
    Jun 2004
    Location
    UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form validation Help

    hi guys newbie here!..just learning form validation

    I have a form with two fields..and have a function which controls the minimum and maximum numbers of characters and upon 'submit' if the user input exceeds the maximum there's an error message. I want to output that error message right beneath that field. I cant seem to get it working..
    anywhere i can find tutorial of such kinds of form validation??
    here's the code:
    PHP Code:
    function checkLength($string, $min, $max) {
      $length = strlen ($string);
      if (($length < $min) || ($length > $max)) {
        $error_message = "Your Name must contain between 1 to 4 characters";
        return FALSE;
      } else {
        return TRUE;
      }
    }
    ?>
      <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
      First Name:<input type="text" name="fname" size="30" value="<?= $fname ?>"><br>
      <?php checkLength($fname14); echo $error_message?>
      Last Name:<input type="text" name="lname" size="30" value="<?= $lname ?>"><br>
      <?php checkLength($lname14); echo $error_message?>
      
    <br><input type="SUBMIT" name="submit" value="Submit">

  2. #2
    SitePoint Enthusiast shref's Avatar
    Join Date
    Nov 2004
    Location
    Egypt, Alexandria
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    see that its working
    but you have to change the outputs
    PHP Code:
     
    function checkLength($string, $min, $max)
    {
    $length = strlen ($string);
    if (($length < $min) || ($length > $max) )
         {
         return true;
         }
    else {
         return false;
         }
    }
    $error_message = "Your Name must contain between 1 to 4 characters";
    ?>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
    First Name:<input type="text" name="fname" size="30" value="<?= $fname ?>"><br>
    <?php if (checkLength($fname14)&& isset($submit)) echo $error_message?>
    Last Name:<input type="text" name="lname" size="30" value="<?= $lname ?>"><br>
    <?php if(checkLength($lname14)&& isset($submit)) echo $error_message?>
    <br><input type="SUBMIT" name="submit" value="Submit">
    it's not good to valdiate with php so you can use java script

    HTML Code:
    <script language="javascript">
    function check(form)
    {
    if(form.fname.value.length > 5){
    				alert("first name should be less than 5");
    				form.fname.focus();
    				form.fname.select();
    				return false;
    		 }
    		 return true;
    }
    </script> 
    <form name=form method=post action="....." onsubmit="return check (this);"> 
     
    <input type=text name=fname >
    </form>
     
    it will be nice to use it
    good luck

  3. #3
    SitePoint Enthusiast weekyd's Avatar
    Join Date
    Jun 2004
    Location
    UK
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks man but i don't know java script

    as for php validation what if i had more than one error messages in my functions and i wanted to output those error messages underneath the field...

    its like this

    function function_name ($argument) {
    if (condition) {
    $error= "error message 1";
    else
    $error.="error message 2";
    }
    }

    and i want to see those error message 1 and 2 under the field..

    can anyone help me plzz

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    week,

    PHP is an excellent validation tool -- especially when a visitor can turn off JavaScript!

    The pseudo-code you're after runs something like this:

    If $_POST array key exists and is the value you set
    {
    read your $_Post array
    set $error to ""
    do validation on each key/value pair
    - if validation fails, $error .= "<br>Error message for key"
    if ($error == "")
    {
    - process data
    - send success page
    - exit
    }
    write original page with $error messages

    If you want to write the error messages under each field, use an error message array instead of a string and test then echo each individual error message under the field output.

    Does that help?

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •