Overview
ValidationResult is an enum that represents the possible outcomes when testing if a phone number is possible. It provides detailed information about why a number might not be valid.
Getting Validation Results
UsePhoneNumberUtil::isPossibleNumberWithReason() to get a ValidationResult:
Validation Results
The following validation results are available:| Result | Value | Description |
|---|---|---|
| IS_POSSIBLE | 0 | The number length matches that of valid numbers for this region |
| INVALID_COUNTRY_CODE | 1 | The number has an invalid country calling code |
| TOO_SHORT | 2 | The number is shorter than all valid numbers for this region |
| TOO_LONG | 3 | The number is longer than all valid numbers for this region |
| IS_POSSIBLE_LOCAL_ONLY | 4 | The number length matches that of local numbers only (may be dialable within an area but lacks information to dial from anywhere) |
| INVALID_LENGTH | 5 | The number is longer than the shortest valid numbers but shorter than the longest, and doesn’t match any valid length for this region |
Usage Examples
Basic Validation
Handling All Validation Results
User-Friendly Error Messages
Form Validation
Type-Specific Validation
Validation vs. Possibility
There’s an important distinction between a number being possible and being valid:
- Possible (
isPossibleNumberWithReason): The number has the correct length and format for the region - Valid (
isValidNumber): The number is possible AND matches known number patterns
Best Practices
Recommended Validation Flow
See Also
- PhoneNumberUtil - Use
isPossibleNumberWithReason()andisValidNumber() - NumberParseException - Exceptions thrown during parsing
- Validation Guide - Complete guide to phone number validation