骨干验证器长度规则不计算特殊字符?

时间:2017-06-02 04:31:48

标签: validation backbone.js backbone.validation.js

我有一个验证规则,其中密码必须是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() }
            ]
        }
    }

1 个答案:

答案 0 :(得分: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>
&#13;
&#13;
&#13;