Web Analytics Made Easy -
StatCounter validation help - CodingForum


No announcement yet.

validation help

  • Filter
  • Time
  • Show
Clear All
new posts

  • validation help

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

  • #2
    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
      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; Jul 6, 2002, 01:18 AM.


      • #4
        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
          ...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) == ) {
          'first method succeeded<br>';
          if ( 
          preg_match("/[^a-zA-Z0-9]/"$teststring) == ) {
          '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.

          De gustibus non est disputandum.


          • #6
            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.