louis_coetzee Posted January 28, 2009 Share Posted January 28, 2009 hi, new to php, can anyone help me, i need a function to check tel no input, to check if it is numeric or text if text, ask to please put in valid no. thanks Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/ Share on other sites More sharing options...
Maq Posted January 28, 2009 Share Posted January 28, 2009 is_numeric($variable) //numeric if (trim($variable) == "")) //empty Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-748981 Share on other sites More sharing options...
genericnumber1 Posted January 28, 2009 Share Posted January 28, 2009 Alphanumeric: <?php function is_alphanumeric($string) { return !preg_match('#[^A-Za-z0-9]#', $string); } Alphabetical: <?php function is_alphabetical($string) { return !preg_match('#[^A-Za-z]#', $string); } Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-748987 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 Alphanumeric: <?php function is_alphanumeric($string) { return !preg_match('#[^A-Za-z0-9]#', $string); } Alphabetical: <?php function is_alphabetical($string) { return !preg_match('#[^A-Za-z]#', $string); } whoa there buddy, step away from the regex. There's no reason to be doing double negatives like that. ^ inside a character class will only match something that is not listed, so you're having to turn around and use ! to do the opposite of that negative. Also, you're only checking for 1 character. The first character. So if a string is '1abc' it will return positive for numeric. You need to add a quantifier and start and end of string assertion. if preg_match('~^[A-Za-z]+$~', $string) // is alpha if preg_match('~^[A-Za-z0-9]+$~', $string) // is alphanumeric you could also use ctype_digit ctype_alnum ctype_alpha Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-749089 Share on other sites More sharing options...
genericnumber1 Posted January 29, 2009 Share Posted January 29, 2009 Actually, give it a try, it will search every character and function as expected until it finds a match (which it shouldn't, because matches are bad in this case), you should know that! But you're right about the double negatives, it can be confusing. As far as your solution goes, I've seen an example where a parser will stop at a new line character that matches between the start-line and end-line character. Eg. "test\n@" will return true for your alphanumeric pattern. I doubt that's true in this case, but I didn't feel up to trying it... hell, I could be mistaken, but I went with what I knew would work. Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-749121 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 Actually, give it a try, it will search every character and function as expected until it finds a match (which it shouldn't, because matches are bad in this case), you should know that! But you're right about the double negatives, it can be confusing. As far as your solution goes, I've seen an example where a parser will stop at a new line character that matches between the start-line and end-line character. Eg. "test\n@" will return true for your alphanumeric pattern. I doubt that's true in this case, but I didn't feel up to trying it... hell, I could be mistaken, but I went with what I knew would work. well damn...upon further inspection, I was wrong even about double negative. Apparently 2 wrongs do make a right. It does search until it finds a match. So if you match return !preg_match('#[^A-Za-z]#', $string); against asdf 2asdf sdf2 2323 you will get false true true true from the negated class, but the ! will invert it. Okay my bad, it does work. But still, you don't need to make it so complicated, lol. As far as mine returning a true on a new line: $string = "test\n@"; if (preg_match('~^[A-Za-z]+$~', $string)) echo "$string true"; else echo "$string false"; echo "<br/>"; if (preg_match('~^[A-Za-z0-9]+$~', $string)) echo "$string true"; else echo "$string false"; output: test @ false test @ false Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-749133 Share on other sites More sharing options...
genericnumber1 Posted January 29, 2009 Share Posted January 29, 2009 So we were both wrong, call it a draw! (I try to add Monty Python references to every day conversation at every opportunity.) Yours is certainly more readable. Quote Link to comment https://forums.phpfreaks.com/topic/142870-check-numeric-or-alphabetic/#findComment-749135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.