SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: Validating name

  1. #1
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Validating name

    How do i validate the name field $_POST['name'] for following

    Maximum of 30 chars.
    Allow - and ' chars as there are name O'Reilly and Mohammad-ShaH
    All other cases invalid

    Another thing if i have <input type="text" value="name" size=30> then whatever the user enters that will be taken as a string or array or that depend on the input???

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $n $_POST['name'];
    if (
    strlen($n) < 31 && preg_match("/^[a-zA-Z'-]+$/"$n)) {
      
    // $n is valid
    }
    else {
      
    // $n is not valid

    When PHP receives the contents of the input field it will receive it as a string, whether it gets it via POST or GET. It would be an array of strings if the name were something like "people[]" and there were other fields with the same name. But you haven't even given that input field a name attribute.

  3. #3
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The input from most form elements will be a string.

    I like to use regular expressions for such things. The code below requires a string, that the string is less than or equal to 30 characters and matches a regular expression.

    The regular expression says match the letters A through Z, a through z, the ' and the - one or more times.

    Code:
    if (strlen($_POST["name"]) > 0 && strlen($_POST["name"]) <= 30 && preg_match("/[A-Za-z'-]+/",$_POST["name"]))
    {
      print "<p>Valid Input</p>";
    } else {
      print "<p>Invalid Input</p>";
    }
    I used these as references:

    http://weblogtoolscollection.com/regex/regex.php

    http://us2.php.net/strlen

  4. #4
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bah! I took too long typing it up!

    Darn you to heck, Raffles!

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hehe, still, it's probably worth checking that the strlen is greater than 0 too, so, krishna, take that into account too.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry

    Quote Originally Posted by Raffles View Post
    PHP Code:
    $n $_POST['name'];
    if (
    strlen($n) < 31 && preg_match("/^[a-zA-Z'-]+$/"$n)) {
      
    // $n is valid
    }
    else {
      
    // $n is not valid

    It works for only one name .
    Now if i have a name like John Bell Rod then it doesnot validate.
    It should validate the spaces, - and ' only and 31 character long only

  7. #7
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's fine. Just add valid characters to the character class in the regex.

    You can add a plain ol' white space between the brackets:

    Code:
    preg_match("/^[a-zA-Z'- ]+$/", $n)
    or you can use \s for white space:

    (I didn't test this, but I think I'm on the right track)

    Code:
    preg_match("/^[a-zA-Z'-\s]+$/", $n)

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think \s will also match tabs and newlines, which is no good for a name. It's better just to use a space, like in the first bit of code darkwater's posted above.

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by darkwater23 View Post

    You can add a plain ol' white space between the brackets:

    Code:
    preg_match("/^[a-zA-Z'- ]+$/", $n)
    You're making the usual error here, darkwater. The above expression will validate a string consisting entirely of spaces.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2007
    Posts
    318
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by stereofrog View Post
    You're making the usual error here, darkwater. The above expression will validate a string consisting entirely of spaces.
    Now if i want to validate the content if it has '-,.:;/\! space and ? then the pregmatch should be as below. Please correct if i am on wrong track

    preg_match("/^[a-zA-Z0-9'-,.:;/\!?\s]+$/"

  11. #11
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    preg_match("/^[a-z0-9'\-,.:;\/!?\s]+$/i"$x$m); 
    my mobile portal
    ghiris.ro

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That will still match a string made entirely of whitespace, like stereofrog pointed out above. Perhaps it's easiest to check first if it's all whitespace by using trim and checking the strlen before doing the preg_match.

  13. #13
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about
    PHP Code:
    preg_match("/^[a-z0-9'\-,.:;\/!?]+$/i"$x$m); 
    my mobile portal
    ghiris.ro

  14. #14
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Right, but now if there are ANY spaces the expression will not match the string. That's why your first one is OK, but check first to see if it's all whitespace. Also, instead of \s, I would use a normal space, because I don't think newlines and tabs should be matched.

  15. #15
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Right, but now if there are ANY spaces the expression will not match the string. That's why your first one is OK, but check first to see if it's all whitespace. Also, instead of \s, I would use a normal space, because I don't think newlines and tabs should be matched.
    Yes, you're right
    my mobile portal
    ghiris.ro


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
  •