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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.