Last updated: 3 min ago

Zip Code Validation Swift Example

zip code validation swift example for production applications

javascript
advanced
93% accurate
#geographic
#validation
#regex
#pattern

Pattern Views

46,100
12.5%Last 30 days

Community Rating

93%
350 likes

Regular Expression

/^\d{5}(-\d{4})?$/

Explanation

This regex pattern validates zip code validation swift example. Commonly used by developers for input validation and form checking.

Code Examples

JavaScript Example
javascript
// zip code validation swift example - JavaScript
const regex = /^\d{5}(-\d{4})?$/;

function validate(input) {
  if (!input || typeof input !== 'string') return false;
  return regex.test(input.trim());
}

// Usage examples
console.log(validate('12345')); // true
console.log(validate('1234')); // false
Python Example
python
# zip code validation swift example - Python
import re

pattern = r"^\d{5}(-\d{4})?$"
regex = re.compile(pattern)

def validate(input_str):
    if not input_str or not isinstance(input_str, str):
        return False
    return bool(regex.match(input_str.strip()))

# Usage examples
print(validate('12345'))  # True
print(validate('1234'))  # False
PHP Example
php
<?php
// zip code validation swift example - PHP
function validate($input) {
    if (!is_string($input) || empty(trim($input))) {
        return false;
    }
    
    $pattern = '/^\d{5}(-\d{4})?$/';
    return preg_match($pattern, trim($input)) === 1;
}

// Usage examples
var_dump(validate('12345')); // bool(true)
var_dump(validate('1234')); // bool(false)
?>

Test Cases

Should Match

12345
12345-6789

Should NOT Match

1234
invalid