Contact ID validation improvements

This commit is contained in:
Pinga 2024-10-28 16:00:54 +02:00
parent 9ecd36b5da
commit b026770b9c
2 changed files with 11 additions and 20 deletions

View file

@ -187,19 +187,14 @@ function validate_identifier($identifier) {
$length = strlen($identifier); $length = strlen($identifier);
if ($length < 3) { if ($length < 3 || $length > 16) {
return 'The contact ID seems too short. It should be at least 3 characters long. Please try again.'; return 'Identifier must be between 3 and 16 characters long. Please try again.';
} }
if ($length > 16) { // Updated pattern: allows letters and digits at start and end, hyphens in the middle only
return 'The contact ID seems too long. It should be no more than 16 characters. Please try again.'; $pattern = '/^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/';
}
$pattern1 = '/^[A-Z]+\-[0-9]+$/'; if (!preg_match($pattern, $identifier)) {
$pattern2 = '/^[A-Za-z][A-Z0-9a-z]*$/';
$pattern3 = '/^[a-zA-Z0-9]{16}$/';
if (!preg_match($pattern1, $identifier) && !preg_match($pattern2, $identifier) && !preg_match($pattern3, $identifier)) {
return 'Your contact ID must contain letters (A-Z, a-z), digits (0-9), and optionally a hyphen (-). Please adjust and try again.'; return 'Your contact ID must contain letters (A-Z, a-z), digits (0-9), and optionally a hyphen (-). Please adjust and try again.';
} }
} }

View file

@ -169,19 +169,15 @@ function validate_identifier($identifier) {
$length = strlen($identifier); $length = strlen($identifier);
if ($length < 3) { if ($length < 3 || $length > 16) {
return 'Identifier type minLength value=3, maxLength value=16'; return 'Identifier must be between 3 and 16 characters long';
} }
if ($length > 16) { // Updated pattern: allows letters and digits at start and end, hyphens in the middle only
return 'Identifier type minLength value=3, maxLength value=16'; $pattern = '/^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/';
}
$pattern1 = '/^[A-Z]+\-[0-9]+$/'; if (!preg_match($pattern, $identifier)) {
$pattern2 = '/^[A-Za-z][A-Z0-9a-z]*$/'; return 'The ID must start and end with a letter or digit and can contain hyphens (-) in the middle.';
if (!preg_match($pattern1, $identifier) && !preg_match($pattern2, $identifier)) {
return 'The ID of the contact must contain letters (A-Z) (ASCII), hyphen (-), and digits (0-9).';
} }
} }