www.webdeveloper.com
Results 1 to 9 of 9

Thread: validating a form with php

  1. #1
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602

    validating a form with php

    hi i have validated a form with javascript, and now wish to do it it also with php, i have searched a few places and here but nothing comes up for what im after

    PHP Code:
    if ( $_GET["dob"] != ) {
        echo 
    "Please Enter Your DOB In Format ddmmyy"
        
    exit(); 
    }

    if ( 
    $_GET["dob"] >= 311299 ) {
        echo 
    "Please Enter A Sensible DOB"
        
    exit();

    but these dont work, also if possible i want this field to only accept numbers, i guess using preg_match

    i got it working fine with js, but as i am new or relativly new to php i cannot get this working, its my first form i have validated with, php

    Thanks
    Rich
    Richard Turner - A Virtual Insight

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    When you compare a string to a number, the string will always return as the numeric value of zero. String to number comparison won't work, you need to use strlen() instead.

    To validate it, though, I'd suggest using this RegEx (untested, but should work):


    PHP Code:
    if(isset($_GET["dob"]))
     {
    $dob $_GET["dob"];}

    if(!
    preg_match("/\d{6}/"$dob))
       {die(
    "You must enter a valid date format: MMDDYY.");} 
    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  3. #3
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    ok thanks for the strlen function, i looked past that when reviewing the manual in search for how to do this

    this code you gave me

    if(isset($_GET["dob"]))
    {$dob = $_GET["dob"];}

    if(!preg_match("/d{6}/", $dob))
    {die("You must enter a valid date format: MMDDYY.");}

    returns "you must... mmddyy" whatever the value of dob is

    im using this to check to see the strlen

    $str = $_GET["dob"];

    if(strlen($str) != 6) {
    echo "Please enter your DOB in the format DDMMYY";
    exit();
    } else {
    ....
    }

    and this works fine, theres not a strval(), to check the value of a value, as i could do the same as my above code, but using strval() (or equivalent function) to make sure it is less that 311299, so what function would be capable of doing this? i have had a look at a couple of them but cant seem to find it

    Rich
    Last edited by 96turnerri; 03-29-2004 at 08:35 AM.
    Richard Turner - A Virtual Insight

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413


    PHP Code:
    if(is_numberic($dob) && $dob <= 311299)
      {echo(
    "All's good.");}
     else
      {die(
    "Eww, somethin's wrong...");} 
    [J]ona
    Last edited by Jona; 03-29-2004 at 08:40 AM.
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  5. #5
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    i edited my post
    Richard Turner - A Virtual Insight

  6. #6
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Originally posted by 96turnerri
    i edited my post
    Me too.

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

  7. #7
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    ok thanks jona, im at work atm, but i will try that when i get home as the fields are on my laptop which i forgot

    Rich
    Richard Turner - A Virtual Insight

  8. #8
    Join Date
    Sep 2003
    Location
    Portsmouth, UK Occupation: Web Developer
    Posts
    2,602
    ok that didnt work

    if(is_numberic($dob) && $dob <= 311299)
    {echo("All's good.");}
    else
    {die("Eww, somethin's wrong...");}

    so intsead of that i used

    if($dob >= 010100 && $dob <= 311299)
    {echo("All's good.");}
    else
    {die("Eww, somethin's wrong...");}

    therefore it has to be numeric

    Thanks once again, this is starting to become a habit, next time i will bother someone else :P

    Rich
    Richard Turner - A Virtual Insight

  9. #9
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    No problem.

    [J]ona
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles