Validate phone numbers, amounts, and plan selection
Overview
Before sending a top-up request, validate all inputs to prevent errors and ensure a smooth user experience.
Proper validation reduces API errors and improves user experience by catching mistakes early.
Phone Number Validation
Phone numbers must match this pattern: ^0[567][0-9]{8}$Requirements:
Must start with 0
Second digit must be 5, 6, or 7
Exactly 10 digits total
Only numeric characters
Valid Examples
Invalid Examples
✅ "0778037340"
✅ "0665983439"
✅ "0556121212"
❌ "778037340" - Missing leading zero
❌ "+213778037340" - International format
❌ "0778 037 340" - Contains spaces
function validatePhoneNumber(phone) { if (typeof phone !== 'string') { return { valid: false, error: 'Phone number must be a string' }; } phone = phone.trim(); if (!/^0[567][0-9]{8}$/.test(phone)) { return { valid: false, error: 'Invalid phone format. Must be 10 digits starting with 05, 06, or 07' }; } return { valid: true, phone };}
import redef validate_phone_number(phone): if not isinstance(phone, str): return {'valid': False, 'error': 'Phone number must be a string'} phone = phone.strip() if not re.match(r'^0[567][0-9]{8}$', phone): return {'valid': False, 'error': 'Invalid phone format. Must be 10 digits starting with 05, 06, or 07'} return {'valid': True, 'phone': phone}
<?phpfunction validatePhoneNumber($phone) { if (!is_string($phone)) { return ['valid' => false, 'error' => 'Phone number must be a string']; } $phone = trim($phone); if (!preg_match('/^0[567][0-9]{8}$/', $phone)) { return ['valid' => false, 'error' => 'Invalid phone format. Must be 10 digits starting with 05, 06, or 07']; } return ['valid' => true, 'phone' => $phone];}
Plan Validation
Validate that the selected plan exists and is available:
function validatePlan(planCode, plans) { const plan = plans.find(p => p.code === planCode); if (!plan) { return { valid: false, error: `Plan ${planCode} not found` }; } if (!plan.isEnabled) { return { valid: false, error: `Plan ${plan.name} is currently unavailable` }; } return { valid: true, plan };}
def validate_plan(plan_code, plans): plan = next((p for p in plans if p['code'] == plan_code), None) if not plan: return {'valid': False, 'error': f'Plan {plan_code} not found'} if not plan['is_enabled']: return {'valid': False, 'error': f"Plan {plan['name']} is currently unavailable"} return {'valid': True, 'plan': plan}
<?phpfunction validatePlan($planCode, $plans) { $plan = null; foreach ($plans as $p) { if ($p['code'] === $planCode) { $plan = $p; break; } } if (!$plan) { return ['valid' => false, 'error' => "Plan $planCode not found"]; } if (!$plan['is_enabled']) { return ['valid' => false, 'error' => "Plan {$plan['name']} is currently unavailable"]; } return ['valid' => true, 'plan' => $plan];}
Amount Validation
Validate amounts based on plan type (dynamic vs fixed):
function validateAmount(amount, plan) { // Fixed plans use their predefined amount if (plan.type === 'fixed') { return { valid: true, amount: plan.fixedAmount }; } // Dynamic plans require amount within range if (!amount) { return { valid: false, error: 'Amount is required for dynamic plans' }; } const numAmount = Number(amount); if (isNaN(numAmount) || !Number.isInteger(numAmount)) { return { valid: false, error: 'Amount must be an integer' }; } if (numAmount < plan.minAmount) { return { valid: false, error: `Amount must be at least ${plan.minAmount} DZD` }; } if (numAmount > plan.maxAmount) { return { valid: false, error: `Amount cannot exceed ${plan.maxAmount} DZD` }; } return { valid: true, amount: numAmount };}
def validate_amount(amount, plan): # Fixed plans use their predefined amount if plan['type'] == 'fixed': return {'valid': True, 'amount': plan['fixed_amount']} # Dynamic plans require amount within range if amount is None: return {'valid': False, 'error': 'Amount is required for dynamic plans'} try: num_amount = int(amount) except (ValueError, TypeError): return {'valid': False, 'error': 'Amount must be an integer'} if num_amount < plan['min_amount']: return {'valid': False, 'error': f"Amount must be at least {plan['min_amount']} DZD"} if num_amount > plan['max_amount']: return {'valid': False, 'error': f"Amount cannot exceed {plan['max_amount']} DZD"} return {'valid': True, 'amount': num_amount}
<?phpfunction validateAmount($amount, $plan) { // Fixed plans use their predefined amount if ($plan['type'] === 'fixed') { return ['valid' => true, 'amount' => $plan['fixed_amount']]; } // Dynamic plans require amount within range if ($amount === null || $amount === '') { return ['valid' => false, 'error' => 'Amount is required for dynamic plans']; } if (!is_numeric($amount)) { return ['valid' => false, 'error' => 'Amount must be an integer']; } $numAmount = (int)$amount; if ($numAmount < $plan['min_amount']) { return ['valid' => false, 'error' => "Amount must be at least {$plan['min_amount']} DZD"]; } if ($numAmount > $plan['max_amount']) { return ['valid' => false, 'error' => "Amount cannot exceed {$plan['max_amount']} DZD"]; } return ['valid' => true, 'amount' => $numAmount];}