Support

Account

Home Forums General Issues Adding Required Input Parameters for Custom Field

Solving

Adding Required Input Parameters for Custom Field

  • Hi,

    I am trying to add a rule to a custom field of mine that says customers can only use certain characters in that field. For instance, I want to ensure they use a capital letter, but not as the first character. As well as no special characters.

    Is there anyway to set these rules up?

    I need the following parameters for this rule, which is what makes it even trickier.
    PASSWORD REQUIREMENTS
    Must be at least 8 characters long
    Must contain 1 uppercase letter (cannot be first or last character)
    Must contain 1 number
    Cannot end in a number or special character

    Thanks!

    Regards,
    The Viracocha Datos Team

  • To do this you use an acf/validate_value filter.

    
    add_filter('acf/validate_value/name=your_field_name', 'your_validate_function_name', 10, 4);
    function your_validate_function_name($valid, $value, $field, $input_name) {
      if ($valid !== true) {
        return $valid;
      }
      if (strlen($value) < 8 // < 8 characters
          || preg_match('/^[A-Z]/', $value) // starts with a cap
          || !preg_match('/[A-Z]/, $value) // contains no caps
          || !preg_match('/[0-9]/', $value) // contains no numbers
          || !preg_match('/[0-9a-zA-Z]$/', $value) // ends is something other than a letter or number) {
        $valid = 'Value is not valid';
      }
      return $valid;
    }
    
  • Hi John,

    Thank you very much for the help!

    I am getting an error on the following line: || !preg_match(‘/[0-9a-zA-Z]$/’, $value) // ends is something other than a letter or number) {
    $valid = ‘Value is not valid’;

    “Your PHP code changes were rolled back due to an error on line 224 of file wp-content/themes/hestia-pro/functions.php. Please fix and try saving again.

    syntax error, unexpected ‘a’ (T_STRING), expecting ‘]'”

  • 
    add_filter('acf/validate_value/name=your_field_name', 'your_validate_function_name', 10, 4);
    function your_validate_function_name($valid, $value, $field, $input_name) {
      if ($valid !== true) {
        return $valid;
      }
      if (strlen($value) < 8 // < 8 characters
          || preg_match('/^[A-Z]/', $value) // starts with a cap
          || !preg_match('/[A-Z]/', $value) // contains no caps
          || !preg_match('/[0-9]/', $value) // contains no numbers
          || !preg_match('/[0-9a-zA-Z]$/', $value) // ends is something other than a letter or number
    			) {
        $valid = 'Value is not valid';
      }
      return $valid;
    }
    
  • I saved this in the functions.php file but it does not seem to work. I just added a product to the cart with a password that doesn’t match the parameters and it went through.

    Is there anything else that may be causing an issue?

    Thank you very much for your help so far. I really appreciate it.

    Regards,
    Jamie

  • I’ve altered it to the following but it is still not working, however it is not returning an error anymore. I think maybe it is not finding the root password field? Any ideas why?

    add_filter(‘acf/validate_value/name=root_password’, ‘root_password’, 10, 4);
    function root_password($valid, $value, $field, $input_name) {
    if ($valid !== true) {
    return $valid;
    }
    if (strlen($value) < 8 // < 8 characters
    || preg_match(‘/^[A-Z]/’, $value) // starts with a cap
    || !preg_match(‘/[A-Z]/’, $value) // contains no caps
    || !preg_match(‘/[0-9]/’, $value) // contains no numbers
    || !preg_match(‘/[0-9a-zA-Z]$/’, $value) // ends is something other than a letter or number
    ) {
    $valid = ‘Value is not valid’;
    }
    return $valid;
    }

    Thanks!

  • I don’t see any reason it should not be working. Try this

    
    add_filter('acf/validate_value/name=your_field_name', 'your_validate_function_name', 10, 4);
    function your_validate_function_name($valid, $value, $field, $input_name) {
      return 'this filter is called';
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.