Sobes.tech
Middle

Testing a field with a requirement: special characters and digits, three of which are mandatory.

sobes.tech AI

Answer from AI

To test a field with the requirement that it must contain special characters and digits, with at least three of them being mandatory, I would create the following test cases:

  • Entering a string without special characters and digits — expect validation error.
  • Entering a string with one digit and two special characters — should pass.
  • Entering a string with three digits, no special characters — should pass.
  • Entering a string with two digits and one special character — should pass.
  • Entering a string with two digits and zero special characters — error.
  • Entering an empty string — error.

Example Python code to check such a condition:

import re

def validate_field(value):
    digits = len(re.findall(r'\d', value))
    specials = len(re.findall(r'[!@#$%^&*(),.?":{}|<>]', value))
    if digits + specials >= 3:
        return True
    return False

# Usage example
print(validate_field("abc12!"))  # True
print(validate_field("abc1"))     # False

This approach allows checking that the total number of digits and special characters is at least three.