all files / directives/ rules.js

100% Statements 62/62
85.71% Branches 18/21
100% Functions 17/17
100% Lines 62/62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147                                                                      10× 10×                                   24× 24×                                                                  
import { avalon, isObject, platform} from '../seed/core'
 
avalon.directive('rules', {
    diff: function (rules) {
        Eif (isObject(rules)) {
            var vdom = this.node
            vdom.rules = platform.toJson(rules)
            return true
        }
    }
})
function isRegExp(value) {
    return avalon.type(value) === 'regexp'
}
var rmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i
var rurl = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/
function isCorrectDate(value) {
    if (typeof value === "string" && value) { //是字符串但不能是空字符
        var arr = value.split("-") //可以被-切成3份,并且第1个是4个字符
        Eif (arr.length === 3 && arr[0].length === 4) {
            var year = ~~arr[0] //全部转换为非负整数
            var month = ~~arr[1] - 1
            var date = ~~arr[2]
            var d = new Date(year, month, date)
            return d.getFullYear() === year && d.getMonth() === month && d.getDate() === date
        }
    }
    return false
}
//https://github.com/adform/validator.js/blob/master/validator.js
avalon.shadowCopy(avalon.validators, {
    pattern: {
        message: '必须匹配{{pattern}}这样的格式',
        get: function (value, field, next) {
            var elem = field.dom
            var data = field.data
            if (!isRegExp(data.pattern)) {
                var h5pattern = elem.getAttribute("pattern")
                data.pattern = new RegExp('^(?:' + h5pattern + ')$')
            }
            next(data.pattern.test(value))
            return value
        }
    },
    digits: {
        message: '必须整数',
        get: function (value, field, next) {//整数
            next(/^\-?\d+$/.test(value))
            return value
        }
    },
    number: {
        message: '必须数字',
        get: function (value, field, next) {//数值
            next(!!value && isFinite(value))// isFinite('') --> true
            return value
        }
    },
    norequired: {
        message: '',
        get: function (value, field, next) {
            next(true)
            return value
        }
    },
    required: {
        message: '必须填写',
        get: function (value, field, next) {
            next(value !== '')
            return value
        }
    },
    equalto: {
        message: '密码输入不一致',
        get: function (value, field, next) {
            var id = String(field.data.equalto)
            var other = avalon(document.getElementById(id)).val() || ""
            next(value === other)
            return value
        }
    },
    date: {
        message: '日期格式不正确',
        get: function (value, field, next) {
            var data = field.data
            if (isRegExp(data.date)) {
                next(data.date.test(value))
            } else {
                next(isCorrectDate(value))
            }
            return value
        }
    },
    url: {
        message: 'URL格式不正确',
        get: function (value, field, next) {
            next(rurl.test(value))
            return value
        }
    },
    email: {
        message: 'email格式不正确',
        get: function (value, field, next) {
            next(rmail.test(value))
            return value
        }
    },
    minlength: {
        message: '最少输入{{minlength}}个字',
        get: function (value, field, next) {
            var num = parseInt(field.data.minlength, 10)
            next(value.length >= num)
            return value
        }
    },
    maxlength: {
        message: '最多输入{{maxlength}}个字',
        get: function (value, field, next) {
            var num = parseInt(field.data.maxlength, 10)
            next(value.length <= num)
            return value
        }
    },
    min: {
        message: '输入值不能小于{{min}}',
        get: function (value, field, next) {
            var num = parseInt(field.data.min, 10)
            next(parseFloat(value) >= num)
            return value
        }
    },
    max: {
        message: '输入值不能大于{{max}}',
        get: function (value, field, next) {
            var num = parseInt(field.data.max, 10)
            next(parseFloat(value) <= num)
            return value
        }
    },
    chs: {
        message: '必须是中文字符',
        get: function (value, field, next) {
            next(/^[\u4e00-\u9fa5]+$/.test(value))
            return value
        }
    }
})