-1

我有一个验证规则,其中密码必须为 6 个字符或更多字符。但是这个验证规则不满足/失败是用户密码包含特殊字符。

例如; 一个密码,例如@ _a&^导致验证器说Password must be 6 or more characters in length.. 也许验证器不计算特殊字符?像这样的密码abc123是可以的,验证器工作(不抱怨)。验证器等密码abc有效并显示错误消息。

如何让验证器计算特殊字符?我只需要推出自己的验证器吗?

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() }
            ]
        }
    }
4

1 回答 1

0

我对其进行了测试,它按预期工作。您共享的代码没有显示问题。

话虽如此,您可能应该使用minLength任何超过 6 个字符的密码,因为您当前的验证不正确。

_.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>

于 2017-06-02T13:12:30.570 回答