【问题标题】:Masked input Plugin with date limit具有日期限制的屏蔽输入插件
【发布时间】:2012-02-24 09:51:30
【问题描述】:

对于 MM/YYYY 格式的日期,我在输入文本字段中使用屏蔽输入。 (http://digitalbush.com/projects/masked-input-plugin/)

我的 javascript 代码:

jQuery(function($){
   $(".date").mask("12/2099");
});

还有 HTML 代码:

<input type="text" class="date">

当我开始写东西时,字段值 12/2099 并且不可能写其他东西。

如果在 javascript 掩码中我写 99/9999,用户可能会写错误的日期......所以我不需要做什么?

我希望用户可以从 1990 年 1 月 1 日到 2099 年 12 月开始写作,可能会增加日期值 + 20 年或类似的值...

【问题讨论】:

    标签: jquery date format


    【解决方案1】:

    好吧,我认为屏蔽输入很好,但我更喜欢验证输入值,因为这类插件通常从内部使用正则表达式。

    我的解决方案可能是:

    (已编辑和测试)

    为消息添加 div

    <form id="frm">
        <input type="text" class="date">
        <div id="msg"></div>
        <input type="submit" value="Click Me"/>
    </form>
    

    js:

         <script type="text/javascript">
    
                function verifyDate(datevalue) {
    
                  var done = false;
    
                  if(datevalue != null || datevalue != ''){
    
                    //split the date as a tmp var
                    var tmp = datevalue.split('/');
    
                    //get the month and year
                    var month = tmp[0];
                    var year = tmp[1];
    
                   if(month >= 1 && month <= 12){
                      if(year >= 1990 && year <= 2099){
    
                       //clean the message
                       clean();
    
                       //finally, allow the user to pass the submit
                       done = true;
    
                      } else {
                        $('#msg').html('Year must be from 1990 - 2099.');
                      }
                   } else {
                      $('#msg').html('Month is invalid.');
                   }
                }
                return done;
              }
    
              function clean() {
                 $('#msg').html('');
              }
    
              jQuery(function($) {
    
                 //mask the input
                 $(".date").mask("12/2099");
    
                 $('#frm').submit(function() {
                    var datevalue = $('.date').val();
                    return verifyDate(datevalue)           
                 });
    
                 $(".date").keyup(function(){
                    //get the date
                    var datevalue = $(this).val();
    
                    //only if the date is full like this: 'xx/xxxx' continue
                    if(datevalue.length == 7) {               
                      verifyDate(datevalue);
                    } else {
                      clean();
                    }
                 });
    
              });
    
        </script>
    

    希望这会有所帮助。

    问候。

    【讨论】:

    • 消息效果很好,你明白我想要什么,但我没有阻止表单提交,当字段清除时显示消息。我开始写作,它对我说信息,直到我写出好的价值。你有什么想法来阻止表格,对于消息来说它并不重要。问候。
    • 让我在 jsfiddle 中尝试一下,我会再过几分钟回来告诉你
    • 你真是个天才!非常感谢奥斯卡。
    【解决方案2】:

    我可以建议其他解决方案。我修改了奥斯卡的解决方案。 我使用了Josh Bush's 屏蔽输入插件。此插件仅用于引导用户输入而无需任何验证。我将领先与验证结合起来。 这是我的代码:

    <script src="@Links.Scripts.MaskedInput_js"></script>
    <script>
    $.mask.definitions['y'] = '[12]';
    $.mask.definitions['m'] = '[01]';
    $.mask.definitions['d'] = '[0-3]';
    $("#BirthDay").mask("d9/m9/y999", { placeholder: " " });
    
    $("#BirthDay").keyup(function () {
        var datevalue = $(this).val();
        if (datevalue.trim().length == 10) {
            verifyDate(datevalue);
        }
    })
    
    function verifyDate(datevalue) {
        var done = false;
        if (datevalue != null || datevalue != '') {
            var tmp = datevalue.split('/');
            var day = tmp[0];
            var month = tmp[1];
            var year = tmp[2];
            var fromYear = 1900;
            var now = new Date();
    
            if (day >= 1 && day <= 31) {
                if (month >= 1 && month <= 12) {
                    if (year >= fromYear && year <= now.getFullYear()) {
                        clean();
                        done = true;
                    } else {
                        $('#msg').html('Year must be between' + " " + fromYear + " - " + now.getFullYear());
                    }
                } else {
                    $('#msg').html('Month is invalid');
                }
            } else {
                $('#msg').html('Day is invalid');
            }
        }
        return done;
    }
    
    function clean() {
        $('#msg').html('');
    }
    </script>
    

    当然,这不是一个全面的客户端日期验证(例如,这里无法验证 2013 年 2 月 30 日这样的日期)。我必须使用该服务器端日期验证。

    不过,这个解决方案减少了客户的错误。

    【讨论】:

    • 我没有使用验证逻辑,但更严格的输入掩码在任何情况下都很有用
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 2017-03-23
    • 2014-04-02
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    相关资源
    最近更新 更多