Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Location
    wycombe
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php validation ereg!

    I am trying to validate entries server side from a form. I was hoping that this would only allow characters of the alphabet, but i seem to get the echo message returned everytime, no matter whether its within a-z or outside.

    Code:
    if (strlen($v) > 0){ // variety not required
      
      if (!ereg("[a-zA-Z]+",$v))
    {echo "This is invalid variety name.";
    exit(0);}
    }
    Can anyone pick any errors or has any alternative solutions. Thanks.

  • #2
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Try this...

    Code:
    if (strlen($v) > 0){ // variety not required
      
      if (preg_match("/[^a-zA-Z]/",$v)) {
          echo "This is invalid variety name.";
          exit(0);
      }
    }

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    if (strlen($v) > 0)  // variety not required
    {
        if (
    ereg('[^A-z]+',$v))
        {
            echo 
    'This is invalid variety name.';
            exit;
        }

    The circumflex will match anything -other than- an alphabetic character.

  • #4
    New Coder
    Join Date
    Jan 2007
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this one:
    PHP Code:
    if ( !preg_match('/^[a-zA-Z\" "]*$/'$name) ) 
       {      
        echo 
    'Name is not Valid Please try again.';
          } 

  • #5
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Oh heck, this could quickly degenerate into a who can come up with the most convoluted regex

    You could also just do...

    Code:
    if(preg_match("/\W/",$v)) {
    but that also allows spaces and underscores in the string.

  • #6
    New to the CF scene
    Join Date
    Mar 2007
    Location
    wycombe
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the help. Its working now.


  •  

    Posting Permissions

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