> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oneclickdz.com/llms.txt
> Use this file to discover all available pages before exploring further.

# الخطوة 2: التحقق من صحة المدخلات

> التحقق من صحة أرقام الهاتف والمبالغ واختيار الباقة

<div dir="rtl">
  ## نظرة عامة

  قبل إرسال طلب الشحن، تحقق من صحة جميع المدخلات لتجنب الأخطاء وضمان تجربة مستخدم سلسة.

  <Note>
    التحقق السليم يقلل من أخطاء API ويحسن تجربة المستخدم من خلال اكتشاف الأخطاء مبكرًا.
  </Note>

  ## التحقق من رقم الهاتف

  يجب أن تتطابق أرقام الهاتف مع هذا النمط: `^0[567][0-9]{8}$`

  **المتطلبات:**

  * يجب أن يبدأ بـ `0`
  * يجب أن يكون الرقم الثاني `5` أو `6` أو `7`
  * 10 أرقام بالضبط
  * أرقام فقط بدون رموز أخرى

  <Tabs>
    <Tab title="أمثلة صحيحة">
      - ✅ `"0778037340"`
      - ✅ `"0665983439"`
      - ✅ `"0556121212"`
    </Tab>

    <Tab title="أمثلة غير صحيحة">
      * ❌ `"778037340"` - الصفر الأول مفقود
      * ❌ `"+213778037340"` - التنسيق الدولي
      * ❌ `"0778 037 340"` - يحتوي على مسافات
    </Tab>
  </Tabs>

  <CodeGroup>
    ```javascript Node.js theme={null}
    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 };
    }
    ```

    ```python Python theme={null}
    import re

    def 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}
    ```

    ```php PHP theme={null}
    <?php

    function 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];
    }
    ```
  </CodeGroup>

  ## التحقق من الباقة

  تحقق من أن الباقة المختارة موجودة ومتاحة:

  <CodeGroup>
    ```javascript Node.js theme={null}
    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 };
    }
    ```

    ```python Python theme={null}
    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}
    ```

    ```php PHP theme={null}
    <?php

    function 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];
    }
    ```
  </CodeGroup>

  ## التحقق من المبلغ

  تحقق من صحة المبالغ بناءً على نوع الباقة (ديناميكية أو ثابتة):

  <CodeGroup>
    ```javascript Node.js theme={null}
    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 };
    }
    ```

    ```python Python theme={null}
    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}
    ```

    ```php PHP theme={null}
    <?php

    function 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];
    }
    ```
  </CodeGroup>

  ## التحقق الكامل

  ادمج جميع عمليات التحقق:

  <CodeGroup>
    ```javascript Node.js theme={null}
    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);
    }
    ```

    ```python Python theme={null}
    def validate_topup_request(data, plans):
        errors = []
        
        # Validate phone
        phone_validation = validate_phone_number(data.get('phone'))
        if not phone_validation['valid']:
            errors.append({'field': 'phone', 'message': phone_validation['error']})
        
        # Validate plan
        plan_validation = validate_plan(data.get('plan_code'), plans)
        if not plan_validation['valid']:
            errors.append({'field': 'plan_code', 'message': plan_validation['error']})
            return {'valid': False, 'errors': errors}
        
        # Validate amount
        amount_validation = validate_amount(data.get('amount'), plan_validation['plan'])
        if not amount_validation['valid']:
            errors.append({'field': 'amount', 'message': amount_validation['error']})
        
        if errors:
            return {'valid': False, 'errors': errors}
        
        return {
            'valid': True,
            'data': {
                'phone': phone_validation['phone'],
                'plan_code': plan_validation['plan']['code'],
                'amount': amount_validation['amount']
            }
        }

    # Usage
    result = validate_topup_request({
        'phone': '0778037340',
        'plan_code': 'PREPAID_DJEZZY',
        'amount': 500
    }, plans)

    if not result['valid']:
        print('Validation errors:', result['errors'])
    else:
        print('Valid request:', result['data'])
    ```

    ```php PHP theme={null}
    <?php

    function validateTopUpRequest($data, $plans) {
        $errors = [];
        
        // Validate phone
        $phoneValidation = validatePhoneNumber($data['phone'] ?? '');
        if (!$phoneValidation['valid']) {
            $errors[] = ['field' => 'phone', 'message' => $phoneValidation['error']];
        }
        
        // Validate plan
        $planValidation = validatePlan($data['plan_code'] ?? '', $plans);
        if (!$planValidation['valid']) {
            $errors[] = ['field' => 'plan_code', 'message' => $planValidation['error']];
            return ['valid' => false, 'errors' => $errors];
        }
        
        // Validate amount
        $amountValidation = validateAmount($data['amount'] ?? null, $planValidation['plan']);
        if (!$amountValidation['valid']) {
            $errors[] = ['field' => 'amount', 'message' => $amountValidation['error']];
        }
        
        if (!empty($errors)) {
            return ['valid' => false, 'errors' => $errors];
        }
        
        return [
            'valid' => true,
            'data' => [
                'phone' => $phoneValidation['phone'],
                'plan_code' => $planValidation['plan']['code'],
                'amount' => $amountValidation['amount']
            ]
        ];
    }

    // Usage
    $result = validateTopUpRequest([
        'phone' => '0778037340',
        'plan_code' => 'PREPAID_DJEZZY',
        'amount' => 500
    ], $plans);

    if (!$result['valid']) {
        print_r($result['errors']);
    } else {
        print_r($result['data']);
    }
    ```
  </CodeGroup>

  ## أفضل الممارسات

  <AccordionGroup>
    <Accordion title="التحقق من جهة العميل" icon="bolt">
      تحقق من المدخلات في واجهة المستخدم قبل إجراء استدعاءات API لتحسين تجربة المستخدم.
    </Accordion>

    <Accordion title="التحقق دائمًا من جهة الخادم" icon="server">
      لا تثق أبدًا بمدخلات العميل. تحقق دائمًا من جهة الخادم.
    </Accordion>

    <Accordion title="تقديم رسائل خطأ واضحة" icon="message">
      اعرض رسائل خطأ محددة تساعد المستخدمين على إصلاح المشكلات.
    </Accordion>

    <Accordion title="تنظيف المدخلات" icon="broom">
      أزل المسافات وقم بتوحيد المدخلات قبل التحقق.
    </Accordion>
  </AccordionGroup>

  ## الخطوات التالية

  <CardGroup cols={2}>
    <Card title="الخطوة 3: إرسال الشحنات" icon="paper-plane" href="/ar/mobile-topup-guides/3-sending-topups">
      تعرف على كيفية إرسال طلبات الشحن
    </Card>

    <Card title="مرجع API" icon="book" href="/ar/api-reference/mobile/send-topup">
      توثيق API الكامل
    </Card>
  </CardGroup>
</div>
