天天看點

【筆記】bootstrapValidator

//驗證
function verifica(form) {
    this.form = form;
    this.fields = null;
    //驗證(頁面初始化的時候執行)
    this.verification = function () {        
        //if (!this.fields) throw new Error("驗證規則(fields)不能為空");
        $(this.form).bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: this.fields
        });
    }
    //是否驗證通過(送出時執行)
    this.isValid = function () {        
        var data = $(this.form).data('bootstrapValidator');
        if (data) {
            data.validate();
            if (!data.isValid()) {
                return false;
            }
        }
        return true;
    }
    //清除驗證效果
    this.clearVeri = function () {
        $(this.form).bootstrapValidator('resetForm', true);//消除驗證效果 
    }
}      
//驗證
var verif = new verifica("#addModal .form-horizontal");
verif.fields = {
    newPassword: {
        message: '驗證失敗',
        validators: {
            notEmpty: {
                message: '不能為空'
            },
            stringLength: {
                min: 6,
                max: 20,
                message: "密碼長度在6到20之間"
            }
        }
    },
    inputNewPassword: {
        message: '密碼重複還沒有驗證',
        validators: {
            notEmpty: {
                message: '密碼重複不能為空'
            },
            stringLength: {
                min: 6,
                max: 20,
                message: '密碼長度在6到20之間'
            },
            identical: {
                field: 'newPassword',
                message: '兩次密碼不同請重新輸入'
            }
        }
    },
    code: {
        message: '編号驗證失敗',
        validators: {
            notEmpty: {
                message: '編号不能為空'
            },
            regexp: {
                regexp: /^[a-zA-Z0-9]+$/,
                message: '編号隻能包含大寫、小寫、數字'
            }
        }
    },
    email: {
        validators: {
            notEmpty: {
                message: '郵箱是必填項'
            },
            emailAddress: {
                message: '郵箱格式不正确'
            }
        }
    }
};      
regexp: {
    regexp: /^[^\u4E00-\u9FA5A-Za-z]+/,
    message: '隻包含數字、符号'
}
regexp: {
    regexp: /^[^\u4e00-\u9fa5]+$/,
    message: '編号不包含中文'
},
regexp: {
    regexp: /^[\u4E00-\u9FA5A-Za-z]+$/,
    message: '隻包含中英文'
}      
  • 學習本是一個不斷抄襲、模仿、練習、創新的過程。
  • 雖然,園中已有本人無法超越的同主題博文,為什麼還是要寫。
  • 對于自己,博文隻是總結。在總結的過程發現問題,解決問題。
  • 對于他人,在此過程如果還能附帶幫助他人,那就再好不過了。
  • 由于部落客能力有限,文中可能存在描述不正确,歡迎指正、補充!
  • 感謝您的閱讀。如果文章對您有用,那麼請輕輕點個贊,以資鼓勵。
  • 工控物聯Q群:995475200