mardi 5 novembre 2019

How To validate form in moodle to accept only phone number start with 966 or 05

I want to validate a textbox to accept only phone number that starts with either 05 or 966. Any ideas how can I do that? My condition does NOT work it will not Validate the entered data.

This is my textbox

$mform->addElement('text', 'phone', get_string('StudentUpdatePhoneNumber'), $attributes);

function validation($data, $files) {
        global $CFG, $DB;

        $errors = parent::validation($data, $files);


        if (empty($data['phone'])) {
            $errors['phone']= get_string('ErrorEmptyPhoneNumber');
        }
        else if ( !is_numeric ($data['phone'])) {
            $errors['phone']= get_string('ErrorNotNumPhoneNumber');
        }

        else if(preg_match("^[966](.*)$", $data['phone'])) {
            $errors['phone']= get_string('ErrorStartWithPhoneNumber');

        }

        return $errors;
      }



Aucun commentaire:

Enregistrer un commentaire