2

I want to validate Bank Account number(123456789) and Routing number(434344343), with custom message=>Please enter 12 digit valid account number.(000123456789)

I used number validator, integer but not working as I expecting. Validator should checks values are numbers and also numbers length. I check this documentation here tutorial-core-validators

 public function rules()
{
    return [
      [['accountnumber'], 'number', 'min' => 12, 'max'=>12],// not wokred
      [['routingnumber'], 'number', 'min' => 9, 'max'=>9], // not wokred
// then I used
  [['accountnumber', 'routingnumber', ], 'integer'] 
]
}

Any suggession?

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
  • This worked for me. `['accountnumber', 'match', 'pattern'=> '/^[0-9]{12}$/i', 'message'=> 'Please enter 12 digit valid account number.(000123456789)' ],` – Muhammad Shahzad Dec 02 '15 at 05:27
  • `['routingnumber', 'match', 'pattern'=> '/^[0-9]{9}$/i', 'message'=> 'Please enter 9 digit valid routing number.(110000000)' ],` – Muhammad Shahzad Dec 02 '15 at 05:28
  • This is not related to your question. Did you link up the bank account using Account number, Name on an account and Routing number? If you did? then how and if not then how can I do it? That may be a silly question. – Anurag Sharma Mar 08 '17 at 05:45

3 Answers3

4

The min and max parameters are for the value, not the lenght, maybe you should save as a string.

['accountnumber', 'string', 'length' => [12, 12]

And also add a regular expression:

['accountnumber', 'match', 'pattern' => '/^[0-9]*$/i']
1

Try this:

public function rules()
{
return [
  [['accountnumber'], 'string', 'min' => 12, 'max'=>12, 'message' => "Please enter 12 digit valid account number"],
  [['routingnumber'], 'string', 'min' => 9, 'max'=>9], 
  [['accountnumber', 'routingnumber', ], 'integer'], 
 ]
}
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
1

Minimum, Maximum Number Validation

 public function rules(){
        return [
            [['accountnumber'],'number','min'=>10],
            [['accountnumber'],'number','max'=>100],
            [['accountnumber'],'number','min'=>10,'max'=>100],
        ];
    }

Minimum, Maximum String Validation

public function rules(){
    return [
        [['min_string'],'string','min'=>10],
        [['max_string'],'string','max'=>10],
        [['min_max_string'],'string','min'=>5,'max'=>10],
        ['min_max_string2', 'string', 'length' => [4, 10]],
    ];
}

Custom Validation

public function rules(){
    return [
        ['custom_validation','custom_function_validation', 'values'=>['One', 'Two']],
    ];
}
public function custom_function_validation($attribute, $params){
    // add custom validation
    if (!in_array($this->$attribute, $params['values'])) 
        $this->addError($attribute,'Custom Validation Error');
}
Amitesh Kumar
  • 3,051
  • 1
  • 26
  • 42