val_fun example

A collection of tutorials and posts that show additional functionality of MATE

Moderator: KarelB

val_fun example

Postby admin » Mon Dec 01, 2008 4:42 pm

Someone asked me to give an example on how to use the val_info option so here it goes.

Code: Select all
$tableColumns['email'] = array('display_text' => 'Email', 'perms' => 'EVCTAXQS','val_fun' => array(&$this,'valEmail'));


Code: Select all
   function valEmail($col,$val,$info)
   {
      if (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $val) || preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$val))
      {
         return true;
      }
      else
      {
         return false;
      }
   }
admin
Site Admin
 
Posts: 1502
Joined: Fri Jul 11, 2008 1:34 am

Re: val_fun example

Postby edtiley » Tue May 19, 2020 7:20 pm

I realize this is an old thread, but I've created a validation function for Vehicle Identification numbers (VIN) following this example.

The problem is that when the user presses TAB to move to the next field the validation is bypassed. If the VIN is invalid and the user presses ENTER, the validation function is called and displays the toolTipMsg, so the validation function works.

How do I get the validation to be called when the user presses TAB???


Code: Select all
      $tableColumns['vin'] = array(
      'display_text' => 'VIN',
               'val_fun' => array(&$this,'validateVin'),
//            'input_info' => 'onfocusout="validateVin();"',
        'perms' => 'AETV'
      );


Later in the class is the code for the validation:

Code: Select all
    public static function validateVin($col,$val,$info){
        $vin=strtoupper($val);
       
        //17 Chars <> I,O,Q - Last 4 Numeric
        if(preg_match('/[A-HJ-NPR-Z0-9]{13}[0-9]{4}/', $vin) === 1){
           
            $sum = self::getNumVal($vin[0]) * 8;
            $sum += self::getNumVal($vin[1]) * 7;
            $sum += self::getNumVal($vin[2]) * 6;
            $sum += self::getNumVal($vin[3]) * 5;
            $sum += self::getNumVal($vin[4]) * 4;
            $sum += self::getNumVal($vin[5]) * 3;
            $sum += self::getNumVal($vin[6]) * 2;
            $sum += self::getNumVal($vin[7]) * 10;
            $sum += self::getNumVal($vin[9]) * 9;
            $sum += self::getNumVal($vin[10]) * 8;
            $sum += self::getNumVal($vin[11]) * 7;
            $sum += self::getNumVal($vin[12]) * 6;
            $sum += self::getNumVal($vin[13]) * 5;
            $sum += self::getNumVal($vin[14]) * 4;
            $sum += self::getNumVal($vin[15]) * 3;
            $sum += self::getNumVal($vin[16]) * 2;
           
            $checksum = $sum % 11; //Modulus
           
            return $checksum == $vin[8] || ($checksum == 10 && $vin[8] == 'X');
        }
          // Create custom validation message and return false
          $this->Editor->showDefaultValidationMsg = false;
          $this->Editor->addTooltipMsg('Not A Valid Vin #');
          return false;

    }
   
    private static function getNumVal($char){
        if(in_array($char, ['A', 'J', '1'])) {return 1;}
        elseif(in_array($char, ['B', 'K', 'S', '2'])) {return 2;}
        elseif(in_array($char, ['C', 'L', 'T', '3'])) {return 3;}
        elseif(in_array($char, ['D', 'M', 'U', '4'])) {return 4;}
        elseif(in_array($char, ['E', 'N', 'V', '5'])) {return 5;}
        elseif(in_array($char, ['F', 'W', '6'])) {return 6;}
        elseif(in_array($char, ['G', 'P', 'X', '7'])) {return 7;}
        elseif(in_array($char, ['H', 'Y', '8'])) {return 8;}
        elseif(in_array($char, ['R', 'Z', '9'])) {return 9;}
        elseif($char == 0) {return 0;}
    }   
   function __construct()
         ...
edtiley
 
Posts: 6
Joined: Wed Oct 16, 2019 8:13 pm


Return to How To

Who is online

Users browsing this forum: No registered users and 3 guests

cron