Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-06-2002, 05:35 AM   PM User | #1
Phip
Registered User

 
Join Date: Jun 2002
Location: Arizona
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Phip is an unknown quantity at this point
validation help

how can i do you check a string to make sure it is only numbers and letters?
Phip is offline   Reply With Quote
Old 07-06-2002, 06:10 AM   PM User | #2
Cloudski
Regular Coder

 
Join Date: Jul 2002
Location: U.S. (Wish Japan though)
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Cloudski is an unknown quantity at this point
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
Cloudski is offline   Reply With Quote
Old 07-06-2002, 06:16 AM   PM User | #3
Phip
Registered User

 
Join Date: Jun 2002
Location: Arizona
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Phip is an unknown quantity at this point
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 06:18 AM..
Phip is offline   Reply With Quote
Old 07-06-2002, 01:18 PM   PM User | #4
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.
Ökii is offline   Reply With Quote
Old 07-06-2002, 03:52 PM   PM User | #5
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
...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
mordred is offline   Reply With Quote
Old 07-07-2002, 02:31 PM   PM User | #6
Ökii
Regular Coder

 
Join Date: Jun 2002
Location: UK
Posts: 577
Thanks: 0
Thanked 0 Times in 0 Posts
Ökii is an unknown quantity at this point
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.
Ökii is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:22 AM.


Advertisement
Log in to turn off these ads.