Validating Chars in a String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ralph Freshour

    Validating Chars in a String

    I'm looking in my PHP book but still figure out the best way to
    validate that certain characters are contained in a string - for
    example, when a user enters a username, I want to limit the characters
    to a..z, A..Z,0..9 only. What's the best way to check for this?

    Thanks...

  • Rudolf Horbas

    #2
    Re: Validating Chars in a String

    > if (!preg_match("# ^([a-zA-Z0-9]+)$#", $useranme) {[color=blue]
    > die('Invalid chars in username!');
    > }[/color]

    A friendlier method than the die would be, of course:

    if (!preg_match("# ^([a-zA-Z0-9]+)$#", $useranme) {
    $error_message = "Invalid chars in username!";
    }

    echo ( $error_message != "" ) ?
    "$error_message "
    :
    "$success_messa ge or whatever the script does afterwards ...";

    rudi

    Comment

    • Ian.H [dS]

      #3
      Re: Validating Chars in a String

      On Fri, 15 Aug 2003 19:27:55 +0200 in
      <message-id:bhj4t8$mem$1 @svr8.m-online.net>
      Rudolf Horbas <rhorbas@hypote xt.de> wrote:
      [color=blue][color=green]
      > > if (!preg_match("# ^([a-zA-Z0-9]+)$#", $useranme) {
      > > die('Invalid chars in username!');
      > > }[/color]
      >
      > A friendlier method than the die would be, of course:
      >
      > if (!preg_match("# ^([a-zA-Z0-9]+)$#", $useranme) {
      > $error_message = "Invalid chars in username!";
      > }
      >
      > echo ( $error_message != "" ) ?
      > "$error_message "
      > :
      > "$success_messa ge or whatever the script does afterwards
      > ...";
      >
      > rudi
      >[/color]


      Naturally rudi.. mine was a quick demo example.. and I wouldn't
      implement a plain die() call like that for a live application =)



      Regards,

      Ian

      --
      Ian.H [Design & Development]
      digiServ Network - Web solutions
      www.digiserv.net | irc.digiserv.ne t | forum.digiserv. net
      Programming, Web design, development & hosting.

      Comment

      • Andy Hassall

        #4
        Re: Validating Chars in a String

        On Fri, 15 Aug 2003 14:18:11 GMT, "Ian.H [dS]" <ian@WINDOZEdig iserv.net> wrote:
        [color=blue]
        >On Fri, 15 Aug 2003 13:32:31 GMT in
        ><message-id:08opjv4f8qt2 4s8e76vet6jurdg jtl9d2p@4ax.com >
        >Ralph Freshour <ralph@primemai l.com> wrote:
        >[color=green]
        >> I'm looking in my PHP book but still figure out the best way to
        >> validate that certain characters are contained in a string - for
        >> example, when a user enters a username, I want to limit the characters
        >> to a..z, A..Z,0..9 only. What's the best way to check for this?[/color]
        >
        > if (!preg_match("# ^([a-zA-Z0-9]+)$#", $useranme) {
        > die('Invalid chars in username!');
        > }[/color]

        Or just:

        if (preg_match('/[^A-Za-z0-9]/', $username) {

        No need to capture anything, so lose the brackets, and since you're looking
        for bad characters it can return when it finds any, rather than scanning the
        whole lot to make sure they're all good.

        --
        Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
        Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

        Comment

        Working...