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

Thread: validation help

  1. #1
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validation help

    how can i do you check a string to make sure it is only numbers and letters?

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    U.S. (Wish Japan though)
    Posts
    141
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, this would make sure they were letters... I would find out how to test if they were numbers, and put another if statement inside this one, or add it on....

    If(($variable!>='A' && $varialbe!<='Z')&&($variable!<='a' && $variable!>='z'))
    DO THIS.....

    I hope I helped a little

  • #3
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah...umm...i'll have to stare at that code for a few hours, before it starts to make sense... thanks

    isn't there suppod to be a function??
    Last edited by Phip; 07-06-2002 at 07:18 AM.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if(preg_match(([a-zA-Z0-9]),$teststring)==1) {echo 'yay';}

    Something like that anyway.
    Have a gander through the manual and check out regular
    expressions and the preg_match() function.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...hmh... I get a parse error... seems like your delimiters aren't correct? Anyway, you're right, validating strings is done very efficiently by Regular Expressions. Though your example RegExp is not correct, it would validate a string on the first occurence of a letter or number.

    PHP Code:
    $teststring "foo23foo";

    if ( 
    preg_match("/^[a-zA-Z0-9]+$/"$teststring) == ) {
        echo 
    'first method succeeded<br>';
    }
    if ( 
    preg_match("/[^a-zA-Z0-9]/"$teststring) == ) {
        echo 
    'second method one succeeded';

    You have two possiblities: Either test if the whole string consists from the start to the end only of [a-zA-Z0-9], as done in the first if-statement, or check for the occurrence of a character that is not included in [a-zA-Z0-9], as done in the second if-statement.
    With the first method, your string is ok when you have a match, with the second, the string is only valid when no matches (of forbidden characters) were found, hence you test against 0.

    I'd prefer the first RegExp, since it also makes sure that the string isn't empy, which would pass the second method.

    hth
    mordred

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hehe, yeah perhaps I should have typed the
    something like that anyway bit in bold
    on my suggestion.

    One of these days I'll actually get around to
    learning them there regexs better.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.


  •  

    Posting Permissions

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