Validation Patterns
Normalized Codes
Normalize a code before validating its shape.
Normalized Codes
normalized_code.php
<?php
$code = ;
$normalized = strtoupper(trim($code));
$hasPrefix = str_starts_with($normalized, "AB");
$hasLength = strlen($normalized) === 4;
$status = $hasPrefix && $hasLength ? "valid" : "invalid";
echo "code=" . $code . "\n";
echo "normalized=" . $normalized . "\n";
echo "status=" . $status . "\n";
<?php
$code = ;
$normalized = strtoupper(trim($code));
$hasPrefix = str_starts_with($normalized, "AB");
$hasLength = strlen($normalized) === 4;
$status = $hasPrefix && $hasLength ? "valid" : "invalid";
echo "code=" . $code . "\n";
echo "normalized=" . $normalized . "\n";
echo "status=" . $status . "\n";
<?php
$code = ;
$normalized = strtoupper(trim($code));
$hasPrefix = str_starts_with($normalized, "AB");
$hasLength = strlen($normalized) === 4;
$status = $hasPrefix && $hasLength ? "valid" : "invalid";
echo "code=" . $code . "\n";
echo "normalized=" . $normalized . "\n";
echo "status=" . $status . "\n";
normalized-code
Validation often works better after normalization. This example trims and uppercases a code before checking its expected prefix and length.