Password validation

Labels

This Discussion is public

Notifications

The code snippet submitted by Ravy here doesn't work for me. Specifically, it rejects passwords that do indeed have 6 characters including at least 1 letter and 1 number. My guess is it's because the preg_match() function in PHP 5.2.14, which is provided by my (low cost) hosting service, is different than the one used by WordPress. So, I've been trying to implement my own. Here's what I now have implemented as a Snippet, which doesn't work. Perhaps someone would be willing to look it over and see what I'm doing wrong. my PHP is kind of rusty 🙁

 

//check password for at least 1 number and 1 letter and min of 8 characters
add_filter('frm_validate_field_entry', 'check_user_pass', 10, 3);
function check_user_pass($errors, $posted_field, $posted_value){
if($posted_field->id == 399){ //change 399 to the ID of the password field to validate
$lengthok = 0;
$hasdigit = 0;
$hasalpha = 0;
$lengthok = 0;
// Check length
if(strlen($posted_value) >= 8){
$lengthok = 1;
}

// Check for at least 1 digit
$posted_val_array = str_split($posted_value);
foreach ($posted_val_array as $c){
if(ctype_digit($c)){
$hasdigit = 1;
break;
}
}
// Check for at least 1 alpha
foreach ($posted_val_array as $c){
if(ctype_alpha($c)){
$hasalpha = 1;
break;
}
}
$allok = ($lengthok == 1) && ($hasdigit == 1) && ($hasalpha == 1);
if(!allok){
$errors['field'. $posted_field->id] = 'Password must be at least 8 characters long and must contain at least 1 number and 1 letter. num digits = ' . strlen($posted_value) . ' lengthok = ' . $lengthok . ' hasdigit = ' . $hasdigit . ' hasalpha = ' . $hasalpha;
}
return $errors;
}
}

 

After submitting I noticed an error:  if(!allok({ should be if(!$allok){ . Unfortunately, it still doesn't work.

In order to allow easier testing, I created a new form with only a username and password (with confirmation) in a new page. Also created a new filter & associated function, and copy/pasted the code into it. Strangely, it worked... for a while. Now, it doesn't.

BTW, I've learned that FF Support does not include help in debugging user developed code. I can understand why, but it leaves me thrashing about in the swamp :-(

 

What am I missing?

 

 

This works in my test form with only Username and Password/Confirmation fields. It fails in my more complex Register User form. Don't know why.

 

//check password for at least 1 number and 1 letter and min of 8 characters
add_filter('frm_validate_field_entry', 'check_user_pass_test', 10, 3);
function check_user_pass_test($errors, $posted_field, $posted_value){
if($posted_field->id == 409){ //change 409 to the ID of the password field to validate
$lengthok = 0;
$hasdigit = 0;
$hasalpha = 0;

// Check length
$len = strlen($posted_value);
if($len >= 8){
$lengthok = 1;
}

//Check for at least 1 digit

$posted_val_array = str_split($posted_value);
foreach ($posted_val_array as $c){
if(ctype_digit($c)){
$hasdigit = 1;
break;
}
}

// Check for at least 1 alpha

foreach ($posted_val_array as $c){
if(ctype_alpha($c)){
$hasalpha = 1;
break;
}
}

//$hasdigit = 1; $hasalpha = 1; // debugging
$allok = ($lengthok == 1) && ($hasdigit == 1) && ($hasalpha == 1);
if(!$allok){

$errors['field'. $posted_field->id] = 'Password must be at least 8 characters long and must contain at least 1 number and 1 letter. num digits = ' . strlen($posted_value) . ' lengthok = ' . $lengthok . ' hasdigit = ' . $hasdigit . ' hasalpha = ' . $hasalpha;
}
return $errors;
}
}

Reply to this Discussion