Between validator常用验证规则

1.数字大小验证规则,很常用,不能超过数据库的字符限制吧

between: {
    min: 2,
    max: 100,
    message: 'The number of floors must be between 2 and 100'
 }

2.当你输出完成后调用的方法,value是你输入的值,你对它进行计算或者验证,之后返回true代表验证成功,false失败代表验证失败。你也可以利用它去后台发送ajax请求来验证,只是注意性能,因为请求有点多,需求不大可以设置前端缓存

callback: {
    message: 'Wrong answer',
    callback: function (value, validator, $field) {
        // Determine the numbers which are generated in captchaOperation
        var items = $('#captchaOperation').html().split(' '),
            sum   = parseInt(items[0]) + parseInt(items[2]);
        return value == sum;
    }
}

3.和 field中填写的表单 内容不能一样, 比如不让密码和用户名一样

different: {
    field: 'username',
    message: 'The password cannot be the same as username'
}

4.和 regexp大家都懂的,不解释=!=

regexp: {
    regexp: /^[a-z\s]+$/i,
    message: 'The full name can consist of alphabetical characters and spaces only'
}

5.非空,不解释=!=

notEmpty: {
    message: 'The full name is required'
}

6.字符串长度验证规则

stringLength: {
    max: 50,
    message: 'The full name must be less than 50 characters'
}

7.字符串大小写验证规则

stringCase: {
    message: 'The card holder must be in uppercase',
    'case': 'upper'                //    Can be lower default or upper
}

8.整数验证

integer: {
    message: 'The value is not an integer'
}

9.远程请求

remote: {
    message: 'The username is not available',
    url: '/path/to/backend/'
}

10.验证文件

file: {
    extension: 'jpeg,png',
    type: 'image/jpeg,image/png',
    maxSize: 2048 * 1024,
    message: 'The selected file is not valid'
}

详情请参考http://bootstrapvalidator.vot…
里面有大量使用案例,后续我写的多了也会补充,现在只是暂时做一个记录,以后用一个在这里记一个

    原文作者:Hash
    原文地址: https://segmentfault.com/a/1190000015125340
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞