The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Password validation to include letters and numbers

Labels

This Discussion is public

Notifications

Here's a password validation hook in case someone needs to validate a password that must include 1 number and 1 letter and must be between 6 to 50 characters long:

//check password for at least 1 number and 1 letter and min of 6 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 == 233){ //change 233 to the ID of the password field to validate
if(!preg_match('/^(?=.*d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{6,50}$/', $posted_value)) {
$errors['field'. $posted_field->id] = 'Password must be at least 6 characters long and must contain at least 1 number and 1 letter';
}
}
return $errors;
}

Thanks for sharing! This is a useful function.

where do one insert this code please.

Thanks for sharing.

You'll need to add the code into your theme's function.php file or into a custom plugin. SeeĀ 
https://formidableforms.com/knowledgebase/formidable-hooks/

 

i use this expression to have at least 8 characters, including one number AND one capital letter:
preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$#", $posted_value)

Where do I put the above expression?

Can we name this a patch rather than a hook? A hook survives an update since it is an official (as designed by the programmer) way to enhance the code at a defined location. E.g. Child CSS are hooks.

The first post on this page includes a function on a hook For adding custom validation to a password field. The line nicmare posted is an alternative Regular expression for use in that function.

Hi, great work ravy and nicmare. I am trying to add also special characters !@#$% to the nicmare expression I tried this

if(!preg_match("#.*^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*[#%?!*-_])(?=.*[0-9]).*$#", $posted_value)) {

but it didn't work, it give me error also when all characters type are in the password and this has the right length. Can you please tell me how to add also a special character condition to the password requested? I think this will interested many formidable pro users.

If someone will need this is my solution

if(!preg_match('/^(?=.{8,20})(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*(_|[^w])).+$/', $posted_value))

 

Snippet work for me:

//check password for at least 1 number and 1 letter and min of 6 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 == 233){ //change 233 to the ID of the password field to validate
if(!preg_match('/^(?=.*d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $posted_value)) {
$errors['field'. $posted_field->id] = 'Password must be at least 6 characters long and must contain at least 1 number and 1 letter';
}
}
return $errors;
}

Tried this but couldn't make it work. Here's what I did.

installed & activate Snippet
Added nee Snippet called Check Password. Copy/pasted the above code into it.
Changed the 233 to ID of my field. See attached screenshot

I get the error message no matter what I put in the password field.

What Am I missing?

 


Attachment:

This is a solution that doesn't use preg_match(). It might be useful for those who, like me, have to use a badly outdated PHP version. Or those who, again like me, do not have RegEx rules memorized, or simply prefer readable code.

Full disclosure: This works perfectly in a test form with only a text field and a password field with Conformation. For reasons yet to be determined, it fails in my much more complex Register User form.

See a flaw? Please let me know.

//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;
}
}

 

 

Discussion closed.