Emerald
Emerald

Reputation: 409

Form validation for matches password

I have a form in my website which is multiplying when a user click add button and the form containing password and password confirmation field.

Here is my view for password field :

<div class="col-md-6 form-group">
    <label>PASSWORD</label>
    <input type="password" name="user_password[]" id="user_password" class="form-control" required>
    <div><?php echo form_error('user_password[]');?></div>
</div>
<div class="col-md-6 form-group">
    <label>CONFIRM PASSWORD</label>
    <input type="password" name="user_confirmpass[]" id="user_confirmpass" class="form-control" required>
    <div><?php echo form_error('user_confirmpass[]');?></div>
</div>

And this is how I do to match the password (controller) :

$this->form_validation->set_rules('user_password[]', 'Password', 'trim');
$this->form_validation->set_rules('user_confirmpass[]', 'Confirm Password', 'trim|matches[user_password[]]');

So if I have only one form, my web will be able to match the password entered but if I have two or more form, I will getting error password does not match even though confirm password match with the password field. I am guessing that my validation to match password isn't correct.

Upvotes: 1

Views: 2279

Answers (1)

Mudassar Khani
Mudassar Khani

Reputation: 1479

Change the password field name to password and confirm password to conf_password or confirm_password then in your controller make rules like

 $config=array(
     array(
          field=>'password',
          label=>'Password',
          rules=>'trim|required'
     ),
     array(
         field=>'confirm_password',
          label=>'Confirm Password',
          rules=>'trim|required|matches[password]'
     )
 );

Now set rules

 $this->form_validation->set_rules($config);

 // Check it

 if($this->form_validation->run()==false) ....

Upvotes: 1

Related Questions