Skip to main content

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"
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 };
}

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 };
}

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 };
}

Complete Validation

Combine all validations:
function validateTopUpRequest(data, plans) {
  const errors = [];
  
  // Validate phone
  const phoneValidation = validatePhoneNumber(data.phone);
  if (!phoneValidation.valid) {
    errors.push({ field: 'phone', message: phoneValidation.error });
  }
  
  // Validate plan
  const planValidation = validatePlan(data.planCode, plans);
  if (!planValidation.valid) {
    errors.push({ field: 'planCode', message: planValidation.error });
    return { valid: false, errors };
  }
  
  // Validate amount
  const amountValidation = validateAmount(data.amount, planValidation.plan);
  if (!amountValidation.valid) {
    errors.push({ field: 'amount', message: amountValidation.error });
  }
  
  if (errors.length > 0) {
    return { valid: false, errors };
  }
  
  return {
    valid: true,
    data: {
      phone: phoneValidation.phone,
      planCode: planValidation.plan.code,
      amount: amountValidation.amount
    }
  };
}

// Usage
const result = validateTopUpRequest({
  phone: '0778037340',
  planCode: 'PREPAID_DJEZZY',
  amount: 500
}, plans);

if (!result.valid) {
  console.error('Validation errors:', result.errors);
} else {
  console.log('Valid request:', result.data);
}

Best Practices

Check inputs in the UI before making API calls to improve UX.
Never trust client input. Always validate on the server.
Show specific error messages that help users fix issues.
Remove whitespace and normalize inputs before validation.

Next Steps