SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm trying to validate a field, to ensure not more than say...10 characters. How do you do that in PHP?

    I know it can be done in HTML/Javascript, but I would like a more secured method.

    Thanks once again
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    Feel my RewiredMind KMxRetro's Avatar
    Join Date
    Jan 2001
    Location
    Exeter, Devon, UK
    Posts
    477
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could send the form to another page and check it there. If it has more than 10 characters, the user is returned to the form (already filled in) and if not, it continues.

    Or you could try this string chopper that was given to me by someone on here. Don't remember the name, but thanks!

    Code:
    function chop_string($str){
    if(strlen($str)>10){
    $str=substr($str,0,10)."";
    return $str;
    }
    else {
    return $str;
    }
    }
    If you then say something like:-

    $formfield = chop_string($formfield)

    It'll be chopped to 10 characters.
    The if(strlen etc line checks the length of the string (in this case "if $formfield is more than 10")
    The $str=substr line replaces everything from the 10th character onwards with "" (nothing) so the string is only 10 characters and then return $str puts the string back into $formfield.

    Should work.
    I personally would have gone with HTML/Javascript, why would you be worrying about security? It's only a form field

    I don't know how anyone would get around a Javascript check for that.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks..It works perfectly well! =)

    How do you ensure a field has no spaces?
    Like any function to verify a field doesn't contain spaces?

    Any tutorial/article on such validating?

    Once again, thanks for your help
    Last edited by lynlimz; Apr 14, 2001 at 02:12.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    strstr($field," ");
    Will return false if the string contains no spaces.
    http://www.php.net/manual/en/function.strstr.php

    If you want to replace the spaces:
    str_replace(" ", "_", $field);
    http://www.php.net/manual/en/function.str-replace.php

  5. #5
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks freaksid..

    How do you ensure, say your 'Age' field ocntains only numbers?

    thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How are you at regular expressions? I have avoided learning them I must read a good tutorial. As this is essential to data validation.
    The relevent php function is ereg()
    http://www.php.net/manual/en/function.ereg.php

    However, alternative and probably better solution for a plain number is is:
    is_numeric($filed);
    http://www.php.net/manual/en/function.is-numeric.php

  7. #7
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much!

    I think I'm finally done with my validation of my registration form..lol
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  8. #8
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just a note, on the limiting to ten characters input, why didn't you:

    Code:
    <input type="text" name="textfield" maxlength="10">
    -Peter

  9. #9
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by lynlimz
    I know it can be done in HTML/Javascript, but I would like a more secured method.

    That does it.
    I not only want client-sde verification on this field, I want server-side too.

    thnaks! =)
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  10. #10
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHPBuilder.com has a regular expressions tutorial -http://www.phpbuilder.com/columns/dario19990616.php3

    You could also use JavaScript to do client-side validation of form values before the form is submitted to a PHP script - http://tech.irt.org/articles/js049/index.htm

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While I was reading this post I decided that there has got to be a way that we can create an array of required form elements complete with their own regex and error messages and then automate the error checking process a bit. I came up with this, anyone who has suggestions to make it bettter, I am all ears:

    PHP Code:
    <?
                 
    //Example array element contains fieldname to check, regex pattern and polartiy and error message
        
    $req_fields[] = array("fieldname" => "name",
                              
    "error" => "Bad or Misisng Name only chactaers allowed no spaces",
                              
    "regex_type" => "!",
                              
    "regex" => "^[A-Za-z]+$"
                              
    );

        
    $req_fields[] = array("fieldname" => "email",
                              
    "error" => "Bad or Misisng Email",
                              
    "regex_type" => "",
                              
    "regex" => "(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)"
                              
    );

        
    $req_fields[] = array("fieldname" => "email",
                              
    "error" => "Bad or Misisng Email",
                              
    "regex_type" => "!",
                              
    "regex" => "^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$"
                              
    );

        
    $req_fields[] = array("fieldname" => "phone",
                              
    "error" => "Bad or Misisng Phone",
                              
    "regex_type" => "!",
                              
    "regex" => "^[\.\(\)[:digit:]-]+$"
                              
    );

        if (
    $submit) {
            foreach(
    $req_fields as $key => $val) {
                
    $tmp $req_fields[$key]["fieldname"];
                if (
    $req_fields[$key]["regex_type"] == "!") {
                    if (!
    ereg($req_fields[$key]["regex"], $$tmp)) {
                        
    $errors[] = $req_fields[$key]["error"];
                        }
                    }
                else {
                    if (
    ereg($req_fields[$key]["regex"], $$tmp)) {
                        
    $errors[] = $req_fields[$key]["error"];
                        }
                    }

                }
            }

    if (
    $errors) {
        foreach(
    $errors as $val) {
            print 
    "$val<br>";
            }
        }
    ?>

    <form action="<?=$PHP_SELF?>" method="GET">
    <input type="text" name="name" value="<?=$name?>"><br>
    <input type="text" name="email" value="<?=$email?>"><br>
    <input type="text" name="phone" value="<?=$phone?>"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow..

    thanks freddy...gonna have to go through it thoroughly..

    I've almost completed my validation for my reg form..if you guys wanna take a look...just yell =)
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  13. #13
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bleh!
    I resolved it! haha...

    WOOHOO!
    Last edited by lynlimz; Apr 16, 2001 at 19:02.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein


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
  •