I'm using WPForms Pro and I'd like to add custom requirements to the password field on a form.
I know that the password field has a password strength requirement setting, but I have specific rules I'd like to enforce, which are:
I came up with the following regex for this: ^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,32}$
and wrote to WPForms support, who told me that they can only go as far as giving me a code snippet to enforce a minimum length:
/**
* Require number of characters allowed for an input field
* Apply the class "wpf-char-require" to the field to enable.
*
*/
function wpf_dev_char_require() {
?>
<script type="text/javascript">
jQuery(function($){
$('.wpf-char-require input').attr('minlength',4); // Change number to character requirement (input fields)
});
</script>
<?php
}
add_action( 'wpforms_wp_footer', 'wpf_dev_char_require' );
(I'd add the wpf-char-require
class to the field I want to validate).
How can I modify this to validate using my regex instead of the rule they've given me?
I'm using WPForms Pro and I'd like to add custom requirements to the password field on a form.
I know that the password field has a password strength requirement setting, but I have specific rules I'd like to enforce, which are:
I came up with the following regex for this: ^(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,32}$
and wrote to WPForms support, who told me that they can only go as far as giving me a code snippet to enforce a minimum length:
/**
* Require number of characters allowed for an input field
* Apply the class "wpf-char-require" to the field to enable.
*
*/
function wpf_dev_char_require() {
?>
<script type="text/javascript">
jQuery(function($){
$('.wpf-char-require input').attr('minlength',4); // Change number to character requirement (input fields)
});
</script>
<?php
}
add_action( 'wpforms_wp_footer', 'wpf_dev_char_require' );
(I'd add the wpf-char-require
class to the field I want to validate).
How can I modify this to validate using my regex instead of the rule they've given me?
I'm not sure about the password field but this is how I perform regular expression checks on my username field. I expect the process is very similar for passwords. Note make sure you set the target_form_id
field so it only processes the form you intend. You can find this id on the embedded short code. Ensure you set the field id for the field you want to validate. You can find this on your wpforms properties side panel on the form designer. You can also set your own error display message. It will display soon as your field is invalid and you have tried to submit your form as per normal operation.
The add action and the below code you put into your functions.php
add_action( 'wpforms_process_validate_text', 'custom_wpforms_text_validation', 10, 3 );
Hope it helps someone
function custom_wpforms_text_validation( $field_id, $field_submit, $form_data ) {
// Check if the form ID matches the target form ID
$target_form_id = 104887;
if ( absint( $form_data['id'] ) !== $target_form_id ) {
return;
}
// Define your regular expression
$regex = '/^[a-zA-Z0-9]+$/'; // Example: Alphanumeric characters only
// Specify the field ID to validate
$target_field_id = 1; // Replace with your field ID
if ( $field_id == $target_field_id && ! preg_match( $regex, $field_submit ) ) {
// Add an error message if validation fails
wpforms()->process->errors[$form_data['id']][$field_id] = 'Invalid input. Please use only alphanumeric characters.';
}
}
html goes here
, how would I apply a regex validation with jQuery?". That question could be asked on stackoverflow and reach a much larger number of people – Tom J Nowell ♦ Commented Apr 21, 2022 at 13:35