-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-phone-validation.ts
More file actions
74 lines (61 loc) · 2.36 KB
/
Copy pathtest-phone-validation.ts
File metadata and controls
74 lines (61 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Test file to demonstrate Uganda phone number validation
* This file shows how the validation works with various input formats
*/
import { validateUgandaPhoneNumber, formatUgandaPhoneNumber } from '../src/utils/phoneValidation';
// Test cases for Uganda phone number validation
const testPhoneNumbers = [
// Valid formats
'+256700789990', // Standard format
'+256 700 789 990', // With spaces
'+256-700-789-990', // With hyphens
'0700789990', // National format with leading 0
'256700789990', // Without + but with country code
'700789990', // Just national number
// Invalid formats
'+256 0700789990', // 0 after country code
'+256700789', // Too few digits
'+25670078999012', // Too many digits
'+256800789990', // Invalid prefix (80x)
'+234700789990', // Wrong country code (Nigeria)
'invalid', // Non-numeric
'', // Empty
'+256', // Only country code
];
console.log('=== Uganda Phone Number Validation Test ===\n');
testPhoneNumbers.forEach((phoneNumber, index) => {
console.log(`Test ${index + 1}: "${phoneNumber}"`);
const validation = validateUgandaPhoneNumber(phoneNumber);
const formatted = formatUgandaPhoneNumber(phoneNumber);
console.log(` Formatted: "${formatted}"`);
console.log(` Valid: ${validation.isValid}`);
if (validation.isValid) {
console.log(` Standard Format: ${validation.formattedNumber}`);
console.log(` Network: ${validation.details?.network || 'Unknown'}`);
} else {
console.log(` Error: ${validation.error}`);
}
console.log('');
});
console.log('=== Form Integration Example ===');
console.log('// In your React component:');
console.log(`
import { validateUgandaPhoneNumber, getPhoneNumberHelperText } from '../utils/phoneValidation';
// Yup validation schema
const schema = yup.object({
telephoneNumber: yup
.string()
.required("Telephone number is required")
.test("uganda-phone-format", "Invalid telephone number format", function(value) {
if (!value) return false;
const validation = validateUgandaPhoneNumber(value);
if (!validation.isValid) {
return this.createError({ message: validation.error });
}
return true;
}),
});
// Helper text for form inputs
const helperText = getPhoneNumberHelperText(phoneValue, errorMessage);
`);
export {}; // Make this a module