Between validator常用驗證規則

Hash發表於2019-02-16

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-zs]+$/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…
裡面有大量使用案例,後續我寫的多了也會補充,現在只是暫時做一個記錄,以後用一個在這裡記一個

相關文章