Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Small Problem with Char Validation

Status
Not open for further replies.

Siberd

Technical User
Jun 24, 2001
35
US
I wanna write a quick php strong validation, to check a user entry is alphanumeric only.. i'm using ereg and i can't make it work.. everytime it just ignores what i've put. any ideas?
the code is:

if(ereg("[[:alnum:]]", $uname))
{
do stuff
}
else
{
tell em its not alphanumeric
}
cheers /Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
Ok, the script now partially works, except if the user enters speechmarks, it allows them through, enaces in escape slashes ( \" ). ANyone have any ideas how to get around this?
cheers /Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
also the charactors : send it funky.. it also seems as tho as long as it actually conatins a letter it can have all the funky charactors in it it wants. /Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
This should work on the principle that if it aint alphanumeric only (with escape slashes removed) then it aint right.

if (!ereg("[a-zA-Z0-9]+", stripslashes($uname)))
{
tell em its not alphanumeric
}
else
{
do stuff
}
***************************************
Party on, dudes!
[cannon]
 
nope.. that didn't do it, it stops the first char being the non-palhanumeric.. same as the other method.
I can't see what it shouldn't work.. got me baffeled.. maybe i'll just try and use some kind of CharAt() and for loop method to check each charactor...
Cheers
Siberd
 
nope.. that didn't do it, it stops the first char being the non-palhanumeric..
So you want the first character to be non alpha numberic?
If non alphanumeric what character do you want it to be?
***************************************
Party on, dudes!
[cannon]
 
try this:

if (!ereg("^[a-zA-Z0-9]+$",$login)){
// wrong login
}

this way checks for the alfanum in the begging and end of string.

in Reg Exp: ^ - means begging of string
$ - end of string. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
i need a user-definatable string (IE when signing up for a user account, creating a log in name) to be completley alpha numeric.. with no nasty chars in it anywhere. ie.. l337d0wd would be fine... but l33=d()wd would cause "Invalid username" to be reported back
the code i had "if(ereg("[[:alnum:]]", $uname))" would stop the first charactor being non-alpha numberic (IE it couldn't be @) but it didn't stop speach marks (") or colons :)) and if the non-aplha numeric charactor was in the second slot of the string (IE p@) it would let it through
KarveR's code ( if (!ereg("[a-zA-Z0-9]+", stripslashes($uname))) ) also produced the same results. with the exception, it prevented the " and : problem (as " and : don't fall in a-z A-Z or 0-9)however it would only check the first charactor.
i think the only way i'm going to be able to do this succesfully would be with a for loop, and some javascript, which checks the string, sets the value of a hidden form element, and passes that to the php script, which i can then if-test to see it it's, true or false.
if anyone has any better ideas, please let me know.
Cheers.
/Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
slight change .. the + indicates 1 or mor of any one character in the set.
* indicates any combination of any character in the set. (allegedly) Hopefully this will solve it for you.

if (!ereg("[a-zA-Z0-9]*", stripslashes($uname))) ***************************************
Party on, dudes!
[cannon]
 
with a little messing around.. a change of function, and a bit of theft (thank your KarveR) i managed a workable statement, that does the job, beutifully.

if (preg_match("/^[a-zA-Z0-9]+$/", stripslashes($uname)))

i new perl would come in handy again one day
*sigh of relief*
Thanks for the help fellas.
Cheers. /Sib
programmer in the making
icq: 44167565
e-mail: siberdude@ntlworld.com
 
Learning is a good thing, if it doesn't work you can't possibly break it anyway :) ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top