Compare commits
No commits in common. "c08080753ea300a73d6cb2c1cdf43969a55c6058" and "a1e687154d9c94992e8cea20a0ecc9fc2467f726" have entirely different histories.
c08080753e
...
a1e687154d
46
src/App.tsx
46
src/App.tsx
|
|
@ -2,11 +2,9 @@ import { useState, useEffect } from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
|
||||||
import { CURRICULUM_MODULES } from './constants/modules';
|
import { CURRICULUM_MODULES } from './constants/modules';
|
||||||
import { getCountryRuleByName } from './constants/countries';
|
|
||||||
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
import type { ModuleItem, Currency, UserProfile, VerificationResult, PublicPlan } from './types';
|
||||||
import { API_BASE } from './services/api';
|
import { API_BASE } from './services/api';
|
||||||
import { loadRazorpayScript } from './services/razorpay';
|
import { loadRazorpayScript } from './services/razorpay';
|
||||||
import { cleanPhoneInput, validatePhoneNumber } from './utils/validation';
|
|
||||||
|
|
||||||
// Modular Subcomponents
|
// Modular Subcomponents
|
||||||
import { StoreHeader } from './components/layout/StoreHeader';
|
import { StoreHeader } from './components/layout/StoreHeader';
|
||||||
|
|
@ -213,12 +211,14 @@ export default function App() {
|
||||||
calculatePricing();
|
calculatePricing();
|
||||||
}, [cart, appliedCoupon, currency]);
|
}, [cart, appliedCoupon, currency]);
|
||||||
|
|
||||||
// Update currency and dial code automatically on country selection during registration
|
// Update currency automatically on country selection during registration
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authMode === 'register') {
|
if (authMode === 'register') {
|
||||||
const rule = getCountryRuleByName(authCountry);
|
if (authCountry === 'India') {
|
||||||
setCurrency(rule.currency);
|
setCurrency('INR');
|
||||||
setAuthDialCode(rule.code);
|
} else {
|
||||||
|
setCurrency('USD');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [authCountry, authMode]);
|
}, [authCountry, authMode]);
|
||||||
|
|
||||||
|
|
@ -240,8 +240,11 @@ export default function App() {
|
||||||
setProfileDisplayName(profileRes.data.display_name || '');
|
setProfileDisplayName(profileRes.data.display_name || '');
|
||||||
setProfilePhone(profileRes.data.phone_number || '');
|
setProfilePhone(profileRes.data.phone_number || '');
|
||||||
setProfileCountry(profileRes.data.country || 'India');
|
setProfileCountry(profileRes.data.country || 'India');
|
||||||
const countryRule = getCountryRuleByName(profileRes.data.country || 'India');
|
if (profileRes.data.country === 'India') {
|
||||||
setCurrency(countryRule.currency);
|
setCurrency('INR');
|
||||||
|
} else {
|
||||||
|
setCurrency('USD');
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to fetch user profile:', err);
|
console.error('Failed to fetch user profile:', err);
|
||||||
handleLogout();
|
handleLogout();
|
||||||
|
|
@ -270,15 +273,6 @@ export default function App() {
|
||||||
setProfileError(null);
|
setProfileError(null);
|
||||||
setProfileSuccess(false);
|
setProfileSuccess(false);
|
||||||
|
|
||||||
if (profilePhone && profilePhone.trim()) {
|
|
||||||
const phoneError = validatePhoneNumber(profilePhone, undefined, profileCountry);
|
|
||||||
if (phoneError) {
|
|
||||||
setProfileError(phoneError);
|
|
||||||
setProfileSaving(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await axios.put(`${API_BASE}/api/auth/profile`, {
|
await axios.put(`${API_BASE}/api/auth/profile`, {
|
||||||
full_name: profileDisplayName,
|
full_name: profileDisplayName,
|
||||||
|
|
@ -304,15 +298,6 @@ export default function App() {
|
||||||
setAuthLoading(true);
|
setAuthLoading(true);
|
||||||
setAuthError(null);
|
setAuthError(null);
|
||||||
|
|
||||||
if (authMode === 'register' && authPhone && authPhone.trim()) {
|
|
||||||
const phoneError = validatePhoneNumber(authPhone, authDialCode, authCountry);
|
|
||||||
if (phoneError) {
|
|
||||||
setAuthError(phoneError);
|
|
||||||
setAuthLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const endpoint = authMode === 'login' ? '/api/auth/login' : '/api/auth/register';
|
const endpoint = authMode === 'login' ? '/api/auth/login' : '/api/auth/register';
|
||||||
|
|
||||||
if (authMode === 'register') {
|
if (authMode === 'register') {
|
||||||
|
|
@ -660,16 +645,9 @@ export default function App() {
|
||||||
setCheckoutLoading(false);
|
setCheckoutLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const cleanedPhone = cleanPhoneInput(inputPhone);
|
|
||||||
const phoneError = validatePhoneNumber(cleanedPhone, undefined, loggedInUser?.country || 'India');
|
|
||||||
if (phoneError) {
|
|
||||||
alert(phoneError);
|
|
||||||
setCheckoutLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const res = await axios.put(`${API_BASE}/api/auth/profile`, {
|
const res = await axios.put(`${API_BASE}/api/auth/profile`, {
|
||||||
phone_number: cleanedPhone
|
phone_number: inputPhone
|
||||||
}, {
|
}, {
|
||||||
headers: { 'Authorization': `Bearer ${userToken}` }
|
headers: { 'Authorization': `Bearer ${userToken}` }
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,7 @@ import {
|
||||||
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
XCircle, Sparkles, AlertCircle, CheckCircle, RefreshCw,
|
||||||
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
Phone, Globe, Eye, EyeOff, LogIn, UserPlus
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries';
|
import { COUNTRY_CODES } from '../../constants/countries';
|
||||||
import { cleanPhoneInput } from '../../utils/validation';
|
|
||||||
|
|
||||||
interface AuthModalProps {
|
interface AuthModalProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
|
|
@ -77,7 +76,6 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
onResetPasswordSubmit,
|
onResetPasswordSubmit,
|
||||||
}) => {
|
}) => {
|
||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
const activeRule = getCountryRuleByDialCode(authDialCode);
|
|
||||||
|
|
||||||
if (!isOpen) return null;
|
if (!isOpen) return null;
|
||||||
|
|
||||||
|
|
@ -292,14 +290,7 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<select
|
<select
|
||||||
value={authDialCode}
|
value={authDialCode}
|
||||||
onChange={(e) => {
|
onChange={(e) => setAuthDialCode(e.target.value)}
|
||||||
const newCode = e.target.value;
|
|
||||||
setAuthDialCode(newCode);
|
|
||||||
const rule = getCountryRuleByDialCode(newCode);
|
|
||||||
if (rule && rule.country !== 'Other') {
|
|
||||||
setAuthCountry(rule.country);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
className="w-[90px] bg-slate-950 border border-slate-800 rounded-xl px-3 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||||
>
|
>
|
||||||
{COUNTRY_CODES.map((cc) => (
|
{COUNTRY_CODES.map((cc) => (
|
||||||
|
|
@ -312,17 +303,11 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
<input
|
<input
|
||||||
type="tel"
|
type="tel"
|
||||||
value={authPhone}
|
value={authPhone}
|
||||||
onChange={(e) => setAuthPhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
|
onChange={(e) => setAuthPhone(e.target.value)}
|
||||||
placeholder={activeRule.placeholder}
|
placeholder="9876543210"
|
||||||
maxLength={activeRule.maxLength}
|
|
||||||
className="flex-1 bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
className="flex-1 bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 placeholder:text-slate-700"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-[10px] text-slate-500 font-medium">
|
|
||||||
{activeRule.minLength === activeRule.maxLength
|
|
||||||
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
|
||||||
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|
@ -335,21 +320,14 @@ export const AuthModal: React.FC<AuthModalProps> = ({
|
||||||
<div className="relative">
|
<div className="relative">
|
||||||
<select
|
<select
|
||||||
value={authCountry}
|
value={authCountry}
|
||||||
onChange={(e) => {
|
onChange={(e) => setAuthCountry(e.target.value)}
|
||||||
const newCountry = e.target.value;
|
|
||||||
setAuthCountry(newCountry);
|
|
||||||
const rule = getCountryRuleByName(newCountry);
|
|
||||||
if (rule) {
|
|
||||||
setAuthDialCode(rule.code);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
className="w-full bg-slate-950 border border-slate-800 rounded-xl px-4 py-2.5 text-sm font-semibold text-white focus:outline-none focus:border-indigo-500 appearance-none"
|
||||||
>
|
>
|
||||||
{COUNTRY_CODES.map((cc) => (
|
<option value="India">India (Calculates in INR ₹)</option>
|
||||||
<option key={cc.code} value={cc.country}>
|
<option value="United States">United States (Calculates in USD $)</option>
|
||||||
{cc.country} (Calculates in {cc.currency} {cc.currency === 'INR' ? '₹' : '$'})
|
<option value="United Kingdom">United Kingdom (Calculates in USD $)</option>
|
||||||
</option>
|
<option value="Germany">Germany (Calculates in USD $)</option>
|
||||||
))}
|
<option value="Australia">Australia (Calculates in USD $)</option>
|
||||||
</select>
|
</select>
|
||||||
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-slate-500 text-xs">
|
<div className="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-slate-500 text-xs">
|
||||||
▼
|
▼
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ import {
|
||||||
Eye, EyeOff, RefreshCw, LogOut
|
Eye, EyeOff, RefreshCw, LogOut
|
||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||||
import { COUNTRY_CODES, getCountryRuleByName } from '../../constants/countries';
|
|
||||||
import { cleanPhoneInput } from '../../utils/validation';
|
|
||||||
|
|
||||||
interface ProfileDrawerProps {
|
interface ProfileDrawerProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
|
|
@ -72,7 +70,6 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
}) => {
|
}) => {
|
||||||
const [showOldPassword, setShowOldPassword] = useState(false);
|
const [showOldPassword, setShowOldPassword] = useState(false);
|
||||||
const [showNewPassword, setShowNewPassword] = useState(false);
|
const [showNewPassword, setShowNewPassword] = useState(false);
|
||||||
const activeRule = getCountryRuleByName(profileCountry);
|
|
||||||
|
|
||||||
if (!isOpen || !loggedInUser) return null;
|
if (!isOpen || !loggedInUser) return null;
|
||||||
|
|
||||||
|
|
@ -146,16 +143,10 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
value={profilePhone}
|
value={profilePhone}
|
||||||
onChange={(e) => setProfilePhone(cleanPhoneInput(e.target.value, activeRule.maxLength))}
|
onChange={(e) => setProfilePhone(e.target.value)}
|
||||||
placeholder={activeRule.placeholder}
|
placeholder="+919876543210"
|
||||||
maxLength={activeRule.maxLength}
|
|
||||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
|
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500"
|
||||||
/>
|
/>
|
||||||
<p className="text-[10px] text-slate-400 font-medium">
|
|
||||||
{activeRule.minLength === activeRule.maxLength
|
|
||||||
? `${activeRule.label || activeRule.country}: Requires exactly ${activeRule.maxLength} digits`
|
|
||||||
: `${activeRule.label || activeRule.country}: ${activeRule.minLength} to ${activeRule.maxLength} digits`}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="space-y-1.5">
|
<div className="space-y-1.5">
|
||||||
|
|
@ -163,18 +154,25 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
||||||
<select
|
<select
|
||||||
value={profileCountry}
|
value={profileCountry}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const newCountry = e.target.value;
|
setProfileCountry(e.target.value);
|
||||||
setProfileCountry(newCountry);
|
if (e.target.value === 'India') {
|
||||||
const rule = getCountryRuleByName(newCountry);
|
setCurrency('INR');
|
||||||
setCurrency(rule.currency);
|
} else {
|
||||||
|
setCurrency('USD');
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
|
className="w-full bg-white border border-slate-200 rounded-xl px-3 py-2 text-xs font-semibold text-slate-800 focus:outline-none focus:border-indigo-500"
|
||||||
>
|
>
|
||||||
{COUNTRY_CODES.map((cc) => (
|
<option value="India">India</option>
|
||||||
<option key={cc.code} value={cc.country}>
|
<option value="United States">United States</option>
|
||||||
{cc.country}
|
<option value="United Kingdom">United Kingdom</option>
|
||||||
</option>
|
<option value="Australia">Australia</option>
|
||||||
))}
|
<option value="Germany">Germany</option>
|
||||||
|
<option value="France">France</option>
|
||||||
|
<option value="Japan">Japan</option>
|
||||||
|
<option value="China">China</option>
|
||||||
|
<option value="Singapore">Singapore</option>
|
||||||
|
<option value="Canada">Canada</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,69 +1,23 @@
|
||||||
import type { Currency } from '../types';
|
|
||||||
|
|
||||||
export interface CountryCode {
|
export interface CountryCode {
|
||||||
code: string;
|
code: string;
|
||||||
label: string;
|
label: string;
|
||||||
country: string;
|
country: string;
|
||||||
currency: Currency;
|
|
||||||
minLength: number;
|
|
||||||
maxLength: number;
|
|
||||||
placeholder: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const COUNTRY_CODES: CountryCode[] = [
|
export const COUNTRY_CODES: CountryCode[] = [
|
||||||
{ code: '+91', label: '🇮🇳 +91', country: 'India', currency: 'INR', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
{ code: '+91', label: '🇮🇳 +91', country: 'India' },
|
||||||
{ code: '+1', label: '🇺🇸 +1', country: 'United States', currency: 'USD', minLength: 10, maxLength: 10, placeholder: '10-digit phone number' },
|
{ code: '+1', label: '🇺🇸 +1', country: 'US' },
|
||||||
{ code: '+44', label: '🇬🇧 +44', country: 'United Kingdom', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+44', label: '🇬🇧 +44', country: 'UK' },
|
||||||
{ code: '+61', label: '🇦🇺 +61', country: 'Australia', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+61', label: '🇦🇺 +61', country: 'AU' },
|
||||||
{ code: '+49', label: '🇩🇪 +49', country: 'Germany', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+49', label: '🇩🇪 +49', country: 'DE' },
|
||||||
{ code: '+33', label: '🇫🇷 +33', country: 'France', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+33', label: '🇫🇷 +33', country: 'FR' },
|
||||||
{ code: '+81', label: '🇯🇵 +81', country: 'Japan', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+81', label: '🇯🇵 +81', country: 'JP' },
|
||||||
{ code: '+86', label: '🇨🇳 +86', country: 'China', currency: 'USD', minLength: 11, maxLength: 11, placeholder: '11-digit mobile number' },
|
{ code: '+86', label: '🇨🇳 +86', country: 'CN' },
|
||||||
{ code: '+971', label: '🇦🇪 +971', country: 'UAE', currency: 'USD', minLength: 9, maxLength: 9, placeholder: '9-digit mobile number' },
|
{ code: '+971', label: '🇦🇪 +971', country: 'UAE' },
|
||||||
{ code: '+65', label: '🇸🇬 +65', country: 'Singapore', currency: 'USD', minLength: 8, maxLength: 8, placeholder: '8-digit mobile number' },
|
{ code: '+65', label: '🇸🇬 +65', country: 'SG' },
|
||||||
{ code: '+55', label: '🇧🇷 +55', country: 'Brazil', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+55', label: '🇧🇷 +55', country: 'BR' },
|
||||||
{ code: '+82', label: '🇰🇷 +82', country: 'South Korea', currency: 'USD', minLength: 9, maxLength: 11, placeholder: '9 to 11 digits' },
|
{ code: '+82', label: '🇰🇷 +82', country: 'KR' },
|
||||||
{ code: '+7', label: '🇷🇺 +7', country: 'Russia', currency: 'USD', minLength: 10, maxLength: 10, placeholder: '10-digit mobile number' },
|
{ code: '+7', label: '🇷🇺 +7', country: 'RU' },
|
||||||
{ code: '+27', label: '🇿🇦 +27', country: 'South Africa', currency: 'USD', minLength: 9, maxLength: 10, placeholder: '9 to 10 digits' },
|
{ code: '+27', label: '🇿🇦 +27', country: 'ZA' },
|
||||||
{ code: '+234', label: '🇳🇬 +234', country: 'Nigeria', currency: 'USD', minLength: 10, maxLength: 11, placeholder: '10 to 11 digits' },
|
{ code: '+234', label: '🇳🇬 +234', country: 'NG' },
|
||||||
];
|
];
|
||||||
|
|
||||||
export const DEFAULT_COUNTRY = COUNTRY_CODES[0]; // India (+91)
|
|
||||||
|
|
||||||
export const getCountryRuleByDialCode = (dialCode: string): CountryCode => {
|
|
||||||
return (
|
|
||||||
COUNTRY_CODES.find((c) => c.code === dialCode) || {
|
|
||||||
code: dialCode,
|
|
||||||
label: dialCode,
|
|
||||||
country: 'Other',
|
|
||||||
currency: 'USD',
|
|
||||||
minLength: 6,
|
|
||||||
maxLength: 15,
|
|
||||||
placeholder: 'Phone number',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getCountryRuleByName = (countryName: string): CountryCode => {
|
|
||||||
if (!countryName) return DEFAULT_COUNTRY;
|
|
||||||
const normalized = countryName.toLowerCase();
|
|
||||||
return (
|
|
||||||
COUNTRY_CODES.find(
|
|
||||||
(c) =>
|
|
||||||
c.country.toLowerCase() === normalized ||
|
|
||||||
(normalized.includes('india') && c.country === 'India') ||
|
|
||||||
(normalized.includes('united states') && c.country === 'United States') ||
|
|
||||||
(normalized.includes('united kingdom') && c.country === 'United Kingdom') ||
|
|
||||||
(normalized.includes('germany') && c.country === 'Germany') ||
|
|
||||||
(normalized.includes('australia') && c.country === 'Australia')
|
|
||||||
) || {
|
|
||||||
code: '+91',
|
|
||||||
label: '🇮🇳 +91',
|
|
||||||
country: countryName,
|
|
||||||
currency: normalized === 'india' ? 'INR' : 'USD',
|
|
||||||
minLength: 6,
|
|
||||||
maxLength: 15,
|
|
||||||
placeholder: 'Phone number',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
import { getCountryRuleByDialCode, getCountryRuleByName } from '../constants/countries';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Strips non-numeric / non-phone characters from text in real-time
|
|
||||||
* If maxLength is provided, strictly restricts digit length
|
|
||||||
*/
|
|
||||||
export const cleanPhoneInput = (text: string, maxLength?: number): string => {
|
|
||||||
const trimmedStart = text.trimStart();
|
|
||||||
const hasPlus = trimmedStart.startsWith('+');
|
|
||||||
const rest = hasPlus ? trimmedStart.slice(1) : text;
|
|
||||||
const digitsOnly = rest.replace(/\D/g, '');
|
|
||||||
const boundedDigits = maxLength ? digitsOnly.slice(0, maxLength) : digitsOnly;
|
|
||||||
return hasPlus ? `+${boundedDigits}` : boundedDigits;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validates phone number according to country rules (e.g. India = exactly 10 digits)
|
|
||||||
*/
|
|
||||||
export const validatePhoneNumber = (
|
|
||||||
val?: string | null,
|
|
||||||
dialCode?: string,
|
|
||||||
country?: string
|
|
||||||
): string | null => {
|
|
||||||
if (!val || !val.trim()) return null;
|
|
||||||
const trimmed = val.trim();
|
|
||||||
const digits = trimmed.replace(/\D/g, '');
|
|
||||||
|
|
||||||
if (digits.length === 0) {
|
|
||||||
return 'Please enter a valid phone number with numeric digits.';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resolve country rule by dial code or country name
|
|
||||||
let rule = dialCode ? getCountryRuleByDialCode(dialCode) : undefined;
|
|
||||||
if ((!rule || rule.country === 'Other') && country) {
|
|
||||||
rule = getCountryRuleByName(country);
|
|
||||||
}
|
|
||||||
// Default to India if no specific rule resolved and dial code is +91 or country is India
|
|
||||||
if (!rule || rule.country === 'Other') {
|
|
||||||
if (dialCode === '+91' || (country && country.toLowerCase() === 'india')) {
|
|
||||||
rule = getCountryRuleByDialCode('+91');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule.country !== 'Other') {
|
|
||||||
if (rule.minLength === rule.maxLength) {
|
|
||||||
if (digits.length !== rule.minLength) {
|
|
||||||
return `${rule.country} phone number must be exactly ${rule.minLength} digits.`;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (digits.length < rule.minLength) {
|
|
||||||
return `${rule.country} phone number must be at least ${rule.minLength} digits.`;
|
|
||||||
}
|
|
||||||
if (digits.length > rule.maxLength) {
|
|
||||||
return `${rule.country} phone number cannot exceed ${rule.maxLength} digits.`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// General fallback ITU E.164
|
|
||||||
if (digits.length < 6) {
|
|
||||||
return 'Phone number must contain at least 6 digits.';
|
|
||||||
}
|
|
||||||
if (digits.length > 15) {
|
|
||||||
return 'Phone number cannot exceed 15 digits.';
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const isValidPhoneNumber = (
|
|
||||||
val?: string | null,
|
|
||||||
dialCode?: string,
|
|
||||||
country?: string
|
|
||||||
): boolean => {
|
|
||||||
return validatePhoneNumber(val, dialCode, country) === null;
|
|
||||||
};
|
|
||||||
Loading…
Reference in New Issue