Last updated: 3 min ago

Zip Code Check Java Example

zip code check java example for production applications

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

Pattern Views

47,651
12.5%Last 30 days

Community Rating

93%
131 likes

Regular Expression

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

Explanation

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

Code Examples

JavaScript Example
javascript
// zip code check java 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 check java 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 check java 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)
?>
PHP Example
java
// Zip Code Check Java Example - Java
import java.util.regex.Pattern;

public class RegexValidator {
    private static final Pattern pattern = Pattern.compile("^\d{5}(-\d{4})?$");
    
    public static boolean validate(String input) {
        if (input == null || input.trim().isEmpty()) {
            return false;
        }
        return pattern.matcher(input.trim()).find();
    }
    
    // Usage examples
    public static void main(String[] args) {
        System.out.println(validate("test-input")); // Check pattern match
    }
}

Test Cases

Should Match

12345
12345-6789

Should NOT Match

1234
invalid