Last updated: 3 min ago

Mobile Number Format Java Example

mobile number format java example for production applications

java
beginner
92% accurate
#phone
#validation
#regex
#pattern

Pattern Views

41,896
12.5%Last 30 days

Community Rating

92%
76 likes

Regular Expression

/^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/

Explanation

This regex pattern validates mobile number format java example. Commonly used by developers for input validation and form checking.

Code Examples

JavaScript Example
javascript
// mobile number format java example - JavaScript
const regex = /^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/;

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

// Usage examples
console.log(validate('555-123-4567')); // true
console.log(validate('123')); // false
Python Example
python
# mobile number format java example - Python
import re

pattern = r"^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{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('555-123-4567'))  # True
print(validate('123'))  # False
PHP Example
php
<?php
// mobile number format java example - PHP
function validate($input) {
    if (!is_string($input) || empty(trim($input))) {
        return false;
    }
    
    $pattern = '/^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{4}$/';
    return preg_match($pattern, trim($input)) === 1;
}

// Usage examples
var_dump(validate('555-123-4567')); // bool(true)
var_dump(validate('123')); // bool(false)
?>
PHP Example
java
// Mobile Number Format Java Example - Java
import java.util.regex.Pattern;

public class RegexValidator {
    private static final Pattern pattern = Pattern.compile("^\+?1?[-\.\s]?\(?[0-9]{3}\)?[-\.\s]?[0-9]{3}[-\.\s]?[0-9]{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

555-123-4567
(555) 123-4567

Should NOT Match

123
not-phone