Jump to content

[SOLVED] Alphnumeric Checking


mike177

Recommended Posts

Hi, I am having some trouble with my alphanumeric error checking on passwords with my login script.

I am using the following to check the password, its the last "else if" statment, I thought it would be helpful to include the whole proccess:

/*Password error checking*/
$field = "pass";
if(!$subpass){
	$form->setError($field, "* Password Not Entered");
}
else{
	$subpass = stripslashes($subpass);
	if(strlen($subpass) < {
		$form->setError($field, "* Password Must Contain Between 8-15 Characters");
	}
	else if(strlen($subpass) > 15){
		$form->setError($field, "* Password Must Containe Between 8-15 Characters");
	}
	else if(!ereg("([0-9A-Za-z])", ($subpass = trim($subpass)))){
		$form->setError($field, "* Password Must Be Alphanumeric");
	}
}

Note: I am also trimming the password, I've tried removing the trim but that had no effect. I'm not receiving any error messages, it just enteres the password in the database alphanumeric or not.

 

Any suggestions.

 

Thanks in advance for any help.

Link to comment
Share on other sites

You could use ctype as revraz suggested, but the problem with your first attempt was the wrong regexp:

([0-9A-Za-z])

 

1) Minor note, but the parens aren't needed.

 

2) The problem, that regexp matches a single alphanumeric character.  Add a + to it if you want it to match one or more.

 

[0-9A-Za-z]+

 

(edit) While I'm at it, might as well write the regexp to eliminate all of your if statements:

/^[0-9A-Za-z]{8,15}$/

That matches all alphanumeric strings 8 to 15 characters in length with no beginning or trailing white space.

 

if(!preg_match('/^[0-9A-Za-z]{8,15}$/', $pass)){
  // BAD PASSWORD
}

Link to comment
Share on other sites

You could use ctype as revraz suggested, but the problem with your first attempt was the wrong regexp:

([0-9A-Za-z])

 

1) Minor note, but the parens aren't needed.

 

2) The problem, that regexp matches a single alphanumeric character.  Add a + to it if you want it to match one or more.

 

[0-9A-Za-z]+

 

(edit) While I'm at it, might as well write the regexp to eliminate all of your if statements:

/^[0-9A-Za-z]{8,15}$/

That matches all alphanumeric strings 8 to 15 characters in length with no beginning or trailing white space.

 

if(!preg_match('/^[0-9A-Za-z]{8,15}$/', $pass)){
  // BAD PASSWORD
}

Cheers mate, Works a treat.

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.