fix/standardized-phone-validation-and-billing #86
|
|
@ -92,7 +92,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
|
|
||||||
const handleLocalPhoneChange = (newLocal: string) => {
|
const handleLocalPhoneChange = (newLocal: string) => {
|
||||||
setLocalPhone(newLocal);
|
setLocalPhone(newLocal);
|
||||||
setProfilePhone(formatE164Phone(dialCode, newLocal));
|
setProfilePhone(newLocal ? formatE164Phone(dialCode, newLocal) : '');
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
||||||
|
|
@ -192,6 +192,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
onPhoneChange={handleLocalPhoneChange}
|
onPhoneChange={handleLocalPhoneChange}
|
||||||
theme="light"
|
theme="light"
|
||||||
disabled={profileSaving}
|
disabled={profileSaving}
|
||||||
|
error={profileError && profileError.toLowerCase().includes('phone') ? profileError : null}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react';
|
import { ShoppingCart, ShoppingBag, Trash2, RefreshCw, CreditCard, XCircle } from 'lucide-react';
|
||||||
import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types';
|
import type { ModuleItem, Currency, UserProfile, VerificationResult } from '../../types';
|
||||||
|
import { isValidPhoneNumber } from '../../utils/validation';
|
||||||
|
|
||||||
interface CartDrawerProps {
|
interface CartDrawerProps {
|
||||||
cart: ModuleItem[];
|
cart: ModuleItem[];
|
||||||
|
|
@ -351,12 +352,12 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
|
<span className="text-[10px] uppercase font-black tracking-wider text-slate-400">
|
||||||
Billing Profile
|
Billing Profile
|
||||||
</span>
|
</span>
|
||||||
{!loggedInUser.phone_number && (
|
{(!loggedInUser.phone_number || !isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India')) && (
|
||||||
<button
|
<button
|
||||||
onClick={onUpdateBillingPhone}
|
onClick={onUpdateBillingPhone}
|
||||||
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
|
className="text-[9px] text-indigo-600 hover:text-indigo-500 font-black uppercase tracking-wider underline focus:outline-none"
|
||||||
>
|
>
|
||||||
Add Number
|
{loggedInUser.phone_number ? 'Update Number' : 'Add Number'}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -368,7 +369,11 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
||||||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||||||
<span>Phone:</span>
|
<span>Phone:</span>
|
||||||
<span className="text-slate-800 font-bold">
|
<span className="text-slate-800 font-bold">
|
||||||
{loggedInUser.phone_number || (
|
{loggedInUser.phone_number && isValidPhoneNumber(loggedInUser.phone_number, undefined, loggedInUser.country || 'India') ? (
|
||||||
|
loggedInUser.phone_number
|
||||||
|
) : loggedInUser.phone_number ? (
|
||||||
|
<span className="text-amber-600 font-black italic">Incomplete ({loggedInUser.phone_number})</span>
|
||||||
|
) : (
|
||||||
<span className="text-rose-500 font-black italic">Required for checkout</span>
|
<span className="text-rose-500 font-black italic">Required for checkout</span>
|
||||||
)}
|
)}
|
||||||
</span>
|
</span>
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,38 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
// Strictly numeric, capped at maxLength
|
const inputVal = e.target.value;
|
||||||
const cleaned = cleanPhoneInput(e.target.value, activeRule.maxLength, false);
|
const trimmed = inputVal.trim();
|
||||||
|
|
||||||
|
// Check if input contains an explicit international prefix '+'
|
||||||
|
if (trimmed.startsWith('+')) {
|
||||||
|
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||||
|
const matched = sorted.find((c) => trimmed.startsWith(c.code));
|
||||||
|
if (matched) {
|
||||||
|
if (onDialCodeChange && matched.code !== dialCode) {
|
||||||
|
onDialCodeChange(matched.code, matched.country);
|
||||||
|
}
|
||||||
|
const rest = trimmed.slice(matched.code.length).replace(/\D/g, '');
|
||||||
|
onPhoneChange(rest.slice(0, matched.maxLength));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if input was pasted with dial code digits without '+' (e.g. 919876543210)
|
||||||
|
const dialDigits = activeRule.code.replace(/\D/g, '');
|
||||||
|
const rawDigits = trimmed.replace(/\D/g, '');
|
||||||
|
if (
|
||||||
|
dialDigits &&
|
||||||
|
rawDigits.startsWith(dialDigits) &&
|
||||||
|
rawDigits.length > activeRule.maxLength
|
||||||
|
) {
|
||||||
|
const rest = rawDigits.slice(dialDigits.length);
|
||||||
|
onPhoneChange(rest.slice(0, activeRule.maxLength));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strictly numeric local digits, capped at active country's maxLength
|
||||||
|
const cleaned = cleanPhoneInput(inputVal, activeRule.maxLength, false);
|
||||||
onPhoneChange(cleaned);
|
onPhoneChange(cleaned);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -63,6 +93,10 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// Allow '+' as leading character to support international number paste/typing
|
||||||
|
if (e.key === '+' && (e.currentTarget.selectionStart === 0 || !phone)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Block any non-digit
|
// Block any non-digit
|
||||||
if (!/^\d$/.test(e.key)) {
|
if (!/^\d$/.test(e.key)) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
|
||||||
|
|
@ -51,6 +51,11 @@ export const validatePhoneNumber = (
|
||||||
if ((!rule || rule.country === 'Other') && country) {
|
if ((!rule || rule.country === 'Other') && country) {
|
||||||
rule = getCountryRuleByName(country);
|
rule = getCountryRuleByName(country);
|
||||||
}
|
}
|
||||||
|
// If still not found and input starts with '+', match from COUNTRY_CODES
|
||||||
|
if (!rule && trimmed.startsWith('+')) {
|
||||||
|
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||||
|
rule = sorted.find((c) => trimmed.startsWith(c.code));
|
||||||
|
}
|
||||||
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
||||||
if (!rule || rule.country === 'Other') {
|
if (!rule || rule.country === 'Other') {
|
||||||
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
||||||
|
|
@ -62,12 +67,14 @@ export const validatePhoneNumber = (
|
||||||
const dialDigits = rule.code.replace(/\D/g, '');
|
const dialDigits = rule.code.replace(/\D/g, '');
|
||||||
let localDigits = allDigits;
|
let localDigits = allDigits;
|
||||||
|
|
||||||
// If formatted with country code (e.g. +919876543210 or 919876543210 where total length > rule.maxLength), strip country prefix
|
if (trimmed.startsWith('+')) {
|
||||||
if (
|
// Input includes country code prefix (e.g. +919876543210 or +9198765432)
|
||||||
(trimmed.startsWith('+') || allDigits.length > rule.maxLength) &&
|
// Must strip the dial code to extract the actual local number
|
||||||
allDigits.startsWith(dialDigits) &&
|
if (allDigits.startsWith(dialDigits)) {
|
||||||
allDigits.length === dialDigits.length + rule.minLength
|
localDigits = allDigits.slice(dialDigits.length);
|
||||||
) {
|
}
|
||||||
|
} else if (allDigits.length > rule.maxLength && allDigits.startsWith(dialDigits)) {
|
||||||
|
// Input was formatted with dial digits without '+' and exceeds local maxLength (e.g. 919876543210)
|
||||||
localDigits = allDigits.slice(dialDigits.length);
|
localDigits = allDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,11 +144,15 @@ export const parsePhoneNumber = (
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no '+' prefix, check if digits start with fallback country dial digits
|
// If no '+' prefix, check if digits start with fallback country dial digits and exceed national length
|
||||||
const dialDigits = defaultRule.code.replace(/\D/g, '');
|
const dialDigits = defaultRule.code.replace(/\D/g, '');
|
||||||
const rawDigits = trimmed.replace(/\D/g, '');
|
const rawDigits = trimmed.replace(/\D/g, '');
|
||||||
let local = rawDigits;
|
let local = rawDigits;
|
||||||
if (rawDigits.startsWith(dialDigits) && rawDigits.length > dialDigits.length) {
|
if (
|
||||||
|
dialDigits &&
|
||||||
|
rawDigits.startsWith(dialDigits) &&
|
||||||
|
rawDigits.length > defaultRule.maxLength
|
||||||
|
) {
|
||||||
local = rawDigits.slice(dialDigits.length);
|
local = rawDigits.slice(dialDigits.length);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue