^\+?1?
Optional country code (+1)[-. ]?
Optional separator (dash, dot, or space)\(?([0-9]{3})\)?
Area code (3 digits) with optional parentheses[-. ]?([0-9]{3})
Exchange code (3 digits) with optional separator[-. ]?([0-9]{4})$
Subscriber number (4 digits) with optional separator^1
Must start with 1[3-9]
Second digit must be 3-9\d{9}$
Followed by exactly 9 more digits// US Phone Number validation
const usPhoneRegex = /^\+?1?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
function validateUSPhone(phone) {
return usPhoneRegex.test(phone);
}
// China Mobile Number validation
const chinaPhoneRegex = /^1[3-9]\d{9}$/;
function validateChinaPhone(phone) {
return chinaPhoneRegex.test(phone);
}
// Usage examples
console.log(validateUSPhone("(555) 123-4567")); // true
console.log(validateChinaPhone("13812345678")); // true
import re
# US Phone Number pattern
us_phone_pattern = r'^\+?1?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$'
def validate_us_phone(phone):
return bool(re.match(us_phone_pattern, phone))
# China Mobile Number pattern
china_phone_pattern = r'^1[3-9]\d{9}$'
def validate_china_phone(phone):
return bool(re.match(china_phone_pattern, phone))
# Usage examples
print(validate_us_phone("(555) 123-4567")) # True
print(validate_china_phone("13812345678")) # True
import java.util.regex.Pattern;
public class PhoneValidator {
private static final String US_PHONE_PATTERN =
"^\\+?1?[-. ]?\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
private static final String CHINA_PHONE_PATTERN = "^1[3-9]\\d{9}$";
private static final Pattern usPattern = Pattern.compile(US_PHONE_PATTERN);
private static final Pattern chinaPattern = Pattern.compile(CHINA_PHONE_PATTERN);
public static boolean validateUSPhone(String phone) {
return usPattern.matcher(phone).matches();
}
public static boolean validateChinaPhone(String phone) {
return chinaPattern.matcher(phone).matches();
}
public static void main(String[] args) {
System.out.println(validateUSPhone("(555) 123-4567")); // true
System.out.println(validateChinaPhone("13812345678")); // true
}
}
Input | Type | Expected | Description |
---|---|---|---|
(555) 123-4567 | US Format | ✓ Valid | Standard US format with parentheses |
555-123-4567 | US Format | ✓ Valid | US format with dashes |
555.123.4567 | US Format | ✓ Valid | US format with dots |
+1 555 123 4567 | US Format | ✓ Valid | International US format |
13812345678 | China Mobile | ✓ Valid | China mobile number |
18987654321 | China Mobile | ✓ Valid | China mobile number starting with 189 |
15612345678 | China Mobile | ✓ Valid | China mobile number starting with 156 |
12345678901 | Invalid | ✗ Invalid | Invalid China mobile (starts with 12) |
555-12-4567 | Invalid | ✗ Invalid | Invalid US format (wrong digit count) |
abcd-efg-hijk | Invalid | ✗ Invalid | Non-numeric characters |
Validate phone number format during user registration to ensure users enter valid phone numbers, providing a foundation for SMS verification and future contact.
// Registration form validation
if (!phoneRegex.test(userPhone)) {
showError("Please enter a valid phone number");
}
Validate phone number format before sending SMS verification codes to avoid sending failures due to format errors and improve delivery rates.
// SMS sending verification
const validPhones = phoneList.filter(phone =>
phoneRegex.test(phone)
);
Validate and standardize customer phone numbers in CRM systems to ensure accuracy and consistency of customer contact information.
// Customer information validation
const cleanCustomers = customers.filter(customer =>
phoneRegex.test(customer.phone)
);
Validate phone numbers in different formats based on regions in international applications, supporting phone number validation needs across multiple countries and regions.
// International validation
const regex = country === 'US' ? usPhoneRegex :
country === 'CN' ? chinaPhoneRegex : intlPhoneRegex;
Phone number validation usually fails due to format mismatch. Please ensure: Chinese phone numbers start with 1 and have 11 digits; US numbers can include area codes and separators; international numbers need country codes.
Chinese format: 13812345678, 18987654321
US format: (555) 123-4567, 555-123-4567, +1 555 123 4567
International format: +86 138 1234 5678, +1 555 123 4567
Phone number formats vary greatly between countries. It's recommended to apply corresponding validation rules based on the user's selected country or region, or use internationalized phone number validation libraries.
Recommendation: For international applications, consider using professional libraries like libphonenumber to handle phone number formats from various countries.
Format validation is just the first step. In real applications, you also need to send SMS verification codes to confirm the authenticity of the phone number and user ownership.
// Complete phone number validation process
if (phoneRegex.test(phone)) {
sendSMSVerification(phone);
}
It's recommended to store phone numbers in pure numeric format and add separators when displaying as needed. This facilitates data processing while providing a good user experience.