Jump to content

Archived

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

bcamp1973

validating alpha-numeric password

Recommended Posts

i want to force users to create passwords that are alpha-numeric only.  however, i'm not quite sure how to validate their entry.  i'm guessing preg_replace() is an option, but is it the best one?  Is there a built in PHP function for this? I haven't been able to find it at php.net if there is :(

Share this post


Link to post
Share on other sites
You can use a js regular expression for this:

[code]function checkpw(pass) {
var m = /^[0-9a-zA-z]$/;
if (!pass.match(m)) {
alert("Please use only numbers and letters for your password.");
return false;
} else {
return true;
}
}[/code]

call is by using either a onsubmit on the form, or on blur on the form field:

[code]<input type="password" name="password" size="10" onblur="checkpass(this);">[/code]

Share this post


Link to post
Share on other sites
another problem with that is if the user has javascript turned off...

here's an example using eregi which is case insensitive

[code]<?
$s = "Ssertfrt633";

if (eregi('^[[:alnum:]]{2,}$',$s)) {
echo 'ok';
}else{
echo 'bad';
}

?>[/code]

Share this post


Link to post
Share on other sites

×

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.