擴展JS validate

if ($.validator) {
    //驗證手機號碼
    $.validator.addMethod("phone", function (value, element, params) {
        return /^(1)[0-9]{10}$/.test(value);
    }, "請輸入有效的手機號碼");

    //驗證密碼格式
    $.validator.addMethod("pwdCheck", function (value, element, params) {
        return /^(?=.*[A-Z])(?=.*[0-9])(?=.*[a-z])[A-Za-z0-9]{8,32}$/.test(value);
    }, "密碼格式錯誤");

    //驗證密碼格式-選填
    $.validator.addMethod("pwdCheckOptional", function (value, element, params) {
        return /^$|(^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32})$/.test(value);
    }, "密碼格式爲6-32位數字字母組合");

    //驗證幣數量格式-選填
    $.validator.addMethod("coinAmount", function (value, element, params) {
        return /^\d{1,9}(.\d{1,6})?$/.test(value);
    }, "支持整數9位,小數點後6位");

    //驗證URL地址
    $.validator.addMethod("uri", function (value, element, params) {
        var invokeType = $('input[name="invokeType"]:checked').val();
        if (invokeType == 'URL') {
            return /^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
        }
        if (invokeType == 'DUBBO') {
            return value.indexOf('#') > 0 && value.indexOf('#') < value.length - 1;
        }
    }, "URI地址格式錯誤");

    //驗證理財產品年化率
    $.validator.addMethod("rateCheck", function (value, element, params) {
        return value >= 0 && value < 1;
    }, "年華收益格式錯誤");

    //驗證郵箱,自帶的有問題
    $.validator.addMethod("emailCheck", function (value, element, params) {
        return /^()?([0-9a-zA-Z_][_.0-9a-zA-Z-]{0,31}@([0-9a-zA-Z][0-9a-zA-Z-]{0,30}\.){1,4}[a-zA-Z]{2,4})?$/.test(value);
    }, "郵箱格式錯誤");

    //金額格式-選填
    $.validator.addMethod("decimal", function (value, element, params) {
        return /^\d+(?:\.\d{1,10})?$/.test(value);
    }, "格式錯誤,整數或十位小數");

    //金額格式
    $.validator.addMethod("money", function (value, element, params) {
        return value >= 0;
    }, "金額格式錯誤");

    //大於開始時間
    $.validator.addMethod("greaterThanDate", function (value, element, params) {
        return value > $(params).val();
    }, "結束時間必須大於開始時間");

    //大於最小額度
    $.validator.addMethod("greaterThanAmount", function (value, element, params) {
        return parseFloat(value) >= parseFloat($(params).val());
    }, "最大額度必須大於最小額度");
}
相關文章
相關標籤/搜索