简体   繁体   中英

regular expression for phone valid at class

i have to make valid for mobile phone with this expression

00-972-598-195-871

i make this function

    private function phoneVald($phone) {
    return ereg("/^[0-9]{2}-[0-9]{3}-[0-9]{3}-[0-9]{3}-[0-9]{3}$/",$phone);
}

and this is the condition for the expression

public function setPhone($phone) {

    if ($this-> phoneVald($phone)) {
        $this-> phone = $phone;
    } else {
        echo "<br />Bad Phone Number";
    }
}

when i call it

$mine = new frindContInfo(00-972-598-195-871);

i got Bad Phone Number

is there any problem with my code !?

Your regex is right , try this way to call your method from friendContInfo class. you can use also __construct() to set your $phone on object instantiation.

see here http://php.net/manual/en/language.oop5.decon.php

    class frindContInfo {

    public function setPhone($phone){
      if ($this-> phoneVald($phone)) {
         $this-> phone = $phone;
         } else {
         echo "<br />Bad Phone Number";
         }
      }

    private function phoneVald($phone) 
     {
      return ereg("/^[0-9]{2}-[0-9]{3}-[0-9]{3}-[0-9]{3}-[0-9] 
      {3}$/",$phone);
     }
 }

$object = new frindContInfo();

$object->setPhone('00-972-598-195-871');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM