-1

I have a validation rule where a password must be 6 characters or more. But this validation rule is not satisfied/fails is the users password contains special characters.

For example; a password such as @ _a&^ results in the validator saying Password must be 6 or more characters in length.. Maybe the validator doesn't count special characters? A password such as abc123 is ok and the validator works (doesn't complain). password such as abc the validator works and displays the error message.

How can I get the validator to count special characters? Am I just going to have to roll my own validator?

validation: function () {
        return {
            password:  [
                { required: true, msg: _('Password is required.').translate() }
            ,   { length: 6, msg: _('Password must be 6 or more characters in length.').translate() }
            ]
        ,   password2: [
                { required: true, msg: _('Password is required').translate() }
            ,   { equalTo: 'password', msg: _('Password and Confirm Password do not match').translate() }
            ]
        }
    }
sazr
  • 24,984
  • 66
  • 194
  • 362

1 Answers1

0

I tested it and it works as expected. The code you shared does not show the problem.

That being said, you should probably use minLength since any password longer than 6 characters is incorrect with your current validation.

_.extend(Backbone.Model.prototype, Backbone.Validation.mixin);

var Model = Backbone.Model.extend({
  validation: function() {
    return {
      password: [{
        required: true,
        msg: 'Password is required.'
      }, {
        length: 6,
        msg: 'Password must be 6 or more characters in length.'
      }]
    };
  }
});

var model = new Model({ password: '@ _a&^' });
console.log('@ _a&^', model.validate());
model.set('password','@ _a&');
console.log('@ _a&', model.validate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.validation/0.11.5/backbone-validation-min.js"></script>
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129