Jump to content

Trying to match only alphanumeric strings


cooldude832

Recommended Posts

I am trying to make sure passwords are only alphanumeric so I have

<?php
$pattern = "[^A-Za-z0-9]";
if(eregi($values['password'],$pattern)){
$errors[] = "The <b>password</b> can only contain alphanumeric characters.";
}
?>

so that should return an error if it finds a non Alphanumeric string, but it fails

 

any ideas?

why do people keep on looking regex when there is predefine function that might match letters numbers and email

 

 

people keep on asking how to match letters numbers etch.. and they get regex solution

 

OK LOOK at ctype data they might what you're looking for

I don't know about ereg but preg would be

 

$pattern = "/[^A-Za-z0-9]+/";
if(preg_match($pattern,$values['password'])){
$errors[] = "The <b>password</b> can only contain alphanumeric characters.";
}

<?php
$pat = '[a-zA-Z0-9]+';
$str = 'hello23489jhksgjl';
if (ereg($pat, $str)) {
echo 'it is alphanumeric!';
} else {
echo 'no its not alphanumeric!';
}
?>

 

 

Tested:

http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=2k3octs2

that's a good suggestion teng,

but why we answered in regex solutions was because this was his question

 

yes but i guess better to point to better solution.

i always see threads asking for regex and get an regex solution but in fact that is doable using sting function etc..

@dsaba i dont mean anything about this post  hope you see my point

 

thanks

Ronald(teng)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.