`
asivy
  • 浏览: 10341 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

jquery validate cron

阅读更多
验证cron表达式的js代码:

function isCronExpress(cronExpression){     
        var cronParams = cronExpression.split(" ");     
    
        if (cronParams.length < 6 || cronParams.length > 7) {     
            return false;     
        }
    
	        //CronTrigger cronTrigger = new CronTrigger();     
        //cronTrigger.setCronExpression( cronExpression );     
    
        if (cronParams[3] == "?" || cronParams[5]=="?") {     
            //Check seconds param     
            if (!checkSecondsField(cronParams[0])) {     
                return false;     
            }
            
    
            //Check minutes param     
            if (!checkMinutesField(cronParams[1])) {     
                return false;     
            }     
    
            //Check hours param     
            if (!checkHoursField(cronParams[2])) {     
                return false;     
            }     
    
            //Check day-of-month param     
            if (!checkDayOfMonthField(cronParams[3])) {     
                return false;     
            }     
    
            //Check months param     
            if (!checkMonthsField(cronParams[4])) {     
                return false;     
            }     
    
            //Check day-of-week param     
            if (!checkDayOfWeekField(cronParams[5])) {     
                return false;     
            }     
    
            //Check year param     
            if (cronParams.length == 7) {     
                if (!checkYearField(cronParams[6])) {     
                    return false;     
                }     
            }     
    
            return true;     
        } else {     
            return false;     
        }     
    }     
    
    function checkSecondsField(secondsField) {     
        return checkField(secondsField, 0, 59);     
    }     
    
    
    function checkField(secondsField, minimal, maximal) {     
        if (secondsField.indexOf("-") > -1 ) {     
            var startValue = secondsField.substring(0, secondsField.indexOf( "-" ));     
            var endValue = secondsField.substring(secondsField.indexOf( "-" ) + 1);     
    
            if (!(checkIntValue(startValue, minimal, maximal, true) && checkIntValue(endValue, minimal, maximal, true))) {     
                return false;     
            }     
            try {     
                var startVal = parseInt(startValue, 10);     
                var endVal = parseInt(endValue, 10);     
    
                return endVal > startVal;     
            } catch (e) {     
                return false;     
            }     
        } else if (secondsField.indexOf(",") > -1) {     
            return checkListField(secondsField, minimal, maximal);     
        } else if (secondsField.indexOf( "/" ) > -1) {     
            return checkIncrementField( secondsField, minimal, maximal );     
        } else if (secondsField.indexOf( "*" ) != -1) {     
            return true;     
        } else {     
            return checkIntValue(secondsField, minimal, maximal);     
        }     
    }     
    
    function checkIntValue(value, minimal, maximal, checkExtremity) {     
        try {     
            var val = parseInt(value, 10);     
            //判断是否为整数     
            if (value == val) {     
                if (checkExtremity) {     
                    if (val < minimal || val > maximal) {     
                        return false;     
                    }     
                }     
    
                return true;     
            }     
    
            return false;     
        } catch (e) {     
            return false;     
        }     
    }     
    
    function checkMinutesField(minutesField) {     
        return checkField(minutesField, 0, 59);     
    }     
    
    function checkHoursField(hoursField) {     
        return checkField(hoursField, 0, 23);     
    }     
    
    function checkDayOfMonthField(dayOfMonthField) {     
        if (dayOfMonthField == "?") {     
            return true;     
        }     
    
        if (dayOfMonthField.indexOf("L") >= 0) {     
            return checkFieldWithLetter(dayOfMonthField, "L", 1, 7, -1, -1);     
        } else if ( dayOfMonthField.indexOf("W") >= 0) {     
            return checkFieldWithLetter(dayOfMonthField, "W", 1, 31, -1, -1);     
        } else if (dayOfMonthField.indexOf("C") >= 0) {     
            return checkFieldWithLetter(dayOfMonthField, "C", 1, 31, -1, -1);     
        } else {     
            return checkField( dayOfMonthField, 1, 31 );     
        }     
    }     
    
    
    function checkMonthsField(monthsField) {     
/*        monthsField = StringUtils.replace( monthsField, "JAN", "1" );   
        monthsField = StringUtils.replace( monthsField, "FEB", "2" );   
        monthsField = StringUtils.replace( monthsField, "MAR", "3" );   
        monthsField = StringUtils.replace( monthsField, "APR", "4" );   
        monthsField = StringUtils.replace( monthsField, "MAY", "5" );   
        monthsField = StringUtils.replace( monthsField, "JUN", "6" );   
        monthsField = StringUtils.replace( monthsField, "JUL", "7" );   
        monthsField = StringUtils.replace( monthsField, "AUG", "8" );   
        monthsField = StringUtils.replace( monthsField, "SEP", "9" );   
        monthsField = StringUtils.replace( monthsField, "OCT", "10" );   
        monthsField = StringUtils.replace( monthsField, "NOV", "11" );   
        monthsField = StringUtils.replace( monthsField, "DEC", "12" );*/    
    
        monthsField.replace("JAN", "1");     
        monthsField.replace("FEB", "2");     
        monthsField.replace("MAR", "3");     
        monthsField.replace("APR", "4");     
        monthsField.replace("MAY", "5");     
        monthsField.replace("JUN", "6");     
        monthsField.replace("JUL", "7");     
        monthsField.replace("AUG", "8");     
        monthsField.replace("SEP", "9");     
        monthsField.replace("OCT", "10");     
        monthsField.replace("NOV", "11");     
        monthsField.replace("DEC", "12");     
    
        return checkField(monthsField, 1, 31);     
    }     
    
    function checkDayOfWeekField(dayOfWeekField) {     
/*        dayOfWeekField = StringUtils.replace( dayOfWeekField, "SUN", "1" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "MON", "2" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "TUE", "3" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "WED", "4" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "THU", "5" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "FRI", "6" );   
        dayOfWeekField = StringUtils.replace( dayOfWeekField, "SAT", "7" );*/    
    
        dayOfWeekField.replace("SUN", "1" );     
        dayOfWeekField.replace("MON", "2" );     
        dayOfWeekField.replace("TUE", "3" );     
        dayOfWeekField.replace("WED", "4" );     
        dayOfWeekField.replace("THU", "5" );     
        dayOfWeekField.replace("FRI", "6" );     
        dayOfWeekField.replace("SAT", "7" );             
    
        if (dayOfWeekField == "?") {     
            return true;     
        }     
    
        if (dayOfWeekField.indexOf("L") >= 0) {     
            return checkFieldWithLetter(dayOfWeekField, "L", 1, 7, -1, -1);     
        } else if (dayOfWeekField.indexOf("C") >= 0) {     
            return checkFieldWithLetter(dayOfWeekField, "C", 1, 7, -1, -1);     
        } else if (dayOfWeekField.indexOf("#") >= 0) {     
            return checkFieldWithLetter(dayOfWeekField, "#", 1, 7, 1, 5);     
        } else {     
            return checkField(dayOfWeekField, 1, 7);     
        }     
    }     
    
    function checkYearField(yearField) {     
        return checkField(yearField, 1970, 2099);     
    }     
    
    
    function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore,     
                                          minimalAfter, maximalAfter) {     
        var canBeAlone = false;     
        var canHaveIntBefore = false;     
        var canHaveIntAfter = false;     
        var mustHaveIntBefore = false;     
        var mustHaveIntAfter = false;     
    
        if (letter == "L") {     
            canBeAlone = true;     
            canHaveIntBefore = true;     
            canHaveIntAfter = false;     
            mustHaveIntBefore = false;     
            mustHaveIntAfter = false;     
        }     
        if (letter == "W" || letter == "C") {     
            canBeAlone = false;     
            canHaveIntBefore = true;     
            canHaveIntAfter = false;     
            mustHaveIntBefore = true;     
            mustHaveIntAfter = false;     
        }     
        if (letter == "#") {     
            canBeAlone = false;     
            canHaveIntBefore = true;     
            canHaveIntAfter = true;     
            mustHaveIntBefore = true;     
            mustHaveIntAfter = true;     
        }     
    
        var beforeLetter = "";     
        var afterLetter = "";     
    
        if (value.indexOf(letter) >= 0 ) {     
            beforeLetter = value.substring( 0, value.indexOf(letter));     
        }     
    
        if (!value.endsWith(letter)) {     
            afterLetter = value.substring( value.indexOf( letter ) + 1 );     
        }     
    
        if (value.indexOf(letter) >= 0) {     
            if (letter == value) {     
                return canBeAlone;     
            }     
    
            if (canHaveIntBefore) {     
                if (mustHaveIntBefore && beforeLetter.length == 0) {     
                    return false;     
                }     
    
                if (!checkIntValue(beforeLetter, minimalBefore, maximalBefore, true)){     
                    return false;     
                }     
            } else {     
                if (beforeLetter.length > 0 ) {     
                    return false;     
                }     
            }     
    
            if (canHaveIntAfter) {     
                if ( mustHaveIntAfter && afterLetter.length == 0 ) {     
                    return false;     
                }     
    
                if (!checkIntValue(afterLetter, minimalAfter, maximalAfter, true)) {     
                    return false;     
                }     
            } else {     
                if (afterLetter.length > 0) {     
                    return false;     
                }     
            }     
        }     
    
        return true;     
    }     
    
/*    function checkIntValue(value, minimal, maximal) {   
        return checkIntValue(value, minimal, maximal, true);   
    } */    
    
    function checkIncrementField(value, minimal, maximal) {     
        var start = value.substring(0, value.indexOf("/"));     
    
        var increment = value.substring(value.indexOf("/") + 1);     
    
        if (!("*" == start)) {     
            return checkIntValue(start, minimal, maximal, true) && checkIntValue(increment, minimal, maximal, false);     
        } else {     
            return checkIntValue(increment, minimal, maximal, true);     
        }     
    }     
    
    
    
    function checkListField(value, minimal, maximal ) {     
        var st = value.split(",");     
    
        var values = new Array(st.length);     
    
        for(var j = 0; j < st.length; j++) {     
            values[j] = st[j];     
        }     
    
        var previousValue = -1;     
    
        for (var i= 0; i < values.length; i++) {     
            var currentValue = values[i];     
    
            if (!checkIntValue(currentValue, minimal, maximal, true)) {     
                return false;     
            }     
    
            try {     
                var val = parseInt(currentValue, 10);     
    
                if (val <= previousValue) {     
                    return false;     
                } else {     
                    previousValue = val;     
                }     
            } catch (e) {     
                // we have always an int     
            }     
        }     
    
        return true;     
    }    


原文件见附件
分享到:
评论

相关推荐

    jquery validate 信息气泡提示

    在网页开发中,jQuery Validate 是一个非常常用的验证插件,用于对用户输入的数据进行校验,确保数据的有效性和完整性。这个插件可以帮助开发者创建复杂的表单验证规则,提高用户体验,减少服务器端的压力。结合 ...

    jquery validate例子

    《jQuery Validate插件详解与实例应用》 在Web开发中,表单验证是不可或缺的一环,它能够确保用户输入的数据符合预设的规则,提高数据的准确性和安全性。jQuery Validate是一个强大的JavaScript库,专为jQuery设计...

    jQuery Validate 1.1.2

    jQuery Validate 是一个强大的JavaScript库,专门用于前端表单验证,由jQuery团队开发并维护。它极大地简化了在网页上创建高效、用户友好的验证规则的过程,避免了开发者编写大量重复的验证代码。jQuery Validate ...

    jquery validate依赖包及其帮助文档.rar

    《jQuery Validate插件详解与应用指南》 在Web开发领域,表单验证是不可或缺的一环,它能够确保用户输入的数据符合预设的规则,从而提高数据的准确性和系统的稳定性。jQuery Validate是一个强大的JavaScript库,专...

    jquery validate 表单验证

    首先,`jquery.js`是jQuery的核心库,它是jQuery Validate的基础,没有它,我们无法使用jQuery Validate。确保引入了最新的jQuery版本,以便利用其强大的DOM操作和事件处理能力。 其次,`myvalidate.js`可能是你...

    jquery validate 验证自定义样式

    在本文中,我们将深入探讨如何利用jQuery Validate来创建自定义验证样式。 首先,我们从标题"jquery validate 验证自定义样式"开始。jQuery Validate插件默认提供了一些基本的样式,但这些样式可能不能满足所有设计...

    jquery validate 验证手册

    《jQuery Validate验证手册》是针对JavaScript库jQuery的一个插件,主要功能是提供强大的表单验证功能,帮助开发者创建用户友好的、功能丰富的交互式表单。这个插件基于jQuery库,利用JavaScript的强大功能,简化了...

    jQueryValidate.rar

    《jQuery Validate插件详解及其应用》 在Web开发中,表单验证是不可或缺的一部分,它确保用户输入的数据符合预设的规则,提高了用户体验并减少了服务器端的负担。jQuery Validate插件是实现这一功能的强大工具,它...

    解决jquery validate remote 为什么只验证一次

    解决jquery validate remote 只验证一次的问题

    jquery.validate 版本大全

    jquery.validate.1.9.0.min.js jquery.validate.1.12.0.min.js jquery.validate.1.13.1.min.js jquery.validate.1.16.0.min.js jquery.validate.1.14.0.min.js jquery.validate.1.15.1.min.js jquery.validate....

    jQuery validate 自定义样式、规则

    jQuery Validate 是一个非常流行的JavaScript库,用于在网页表单中进行验证。这个库极大地简化了对用户输入数据的检查,确保在提交之前满足特定的业务规则。在本篇博文中,我们将深入探讨如何自定义jQuery Validate...

    jquery validate的漂亮css样式验证

    `jQuery Validate`是一个非常流行的JavaScript库,用于在前端进行表单验证。这个库极大地简化了对用户输入数据的检查,确保它们符合特定的规则和格式。`jQuery Validate`结合自定义CSS样式,可以让表单验证既实用又...

    jQuery Validate插件验证表单小练

    jQuery Validate 插件是Web开发中广泛使用的工具,主要用于实现前端表单验证,它极大地简化了用户输入数据的检查过程,提供了丰富的验证规则和自定义方法。在这个“jQuery Validate插件验证表单小练”中,我们将深入...

    Jquery Validate修改版

    《jQuery Validate修改版:提升表单验证体验》 在网页开发中,用户输入的验证是不可或缺的一环,它能够确保用户提交的数据符合预期,避免无效数据的产生,从而提高用户体验和服务器端处理效率。jQuery Validate插件...

    jquery validate配合struts2简单整改

    《jQuery Validate与Struts2整合应用详解》 在Web开发中,前端验证用户输入的数据是必不可少的一环,jQuery Validate插件就是一款强大的JavaScript验证工具,它可以帮助我们方便地实现表单验证。与此同时,Struts2...

    Jquery validate和form插件

    **jQuery validate 和 form 插件详解** 在网页开发中,用户界面的交互性和数据验证是不可或缺的部分。jQuery 是一个广泛使用的 JavaScript 库,它简化了 DOM 操作、事件处理和动画效果。`jQuery validate` 和 `...

    jquery validate默认错误提示显示位置修改

    ### 修改jQuery Validate默认错误提示显示位置 在网页开发过程中,表单验证是非常重要的一个环节,它不仅能够提高用户体验,还能确保数据的有效性和安全性。jQuery Validate插件是实现表单验证功能的一个强大工具,...

    jquery.validate表单验证密码完整例子(带密码强度显示)

    首先,`jQuery Validate`的核心功能是验证用户在表单中输入的数据,确保其符合预设的验证规则。例如,我们可以设定密码必须包含字母、数字、特殊字符等要求,以提高安全性。在提供的例子中,它会检查用户输入的密码...

    jquery validate表单验证插件制作注册表单提交验证

    在本文中,我们将深入探讨如何使用jQuery Validate插件来创建一个功能完备的注册表单,进行数据提交前的验证。jQuery Validate是一个强大的JavaScript库,它为HTML表单提供了灵活且易于使用的验证规则,确保用户输入...

    关于jquery validate plugin 指定需要验证对象解决方案

    《关于jQuery Validate Plugin指定需要验证对象的解决方案》 在Web开发中,表单验证是必不可少的一环,确保用户输入的数据符合预设的规则。jQuery Validate Plugin是一款强大的客户端表单验证插件,它使得这一过程...

Global site tag (gtag.js) - Google Analytics