Masked input Plugin with date limit

谁说我不能喝 提交于 2019-12-06 06:51:40

问题


I use masked input on a input text field for a date in the MM/YYYY format. ( http://digitalbush.com/projects/masked-input-plugin/ )

My javascript code:

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

And HTML Code:

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

When I start to write something it take to the field the value 12/2099 and it's impossible to write other thing.

If in javascript mask I write 99/9999, users could write date that is false ... so I don't what I have to do ?

I want to users that they could write starting on 01/1990 until 12/2099, possible adding to the restrict with the value of the date + 20 years or something like that ...


回答1:


Well, I think is good to have the masked input but I prefer to validate the input value cause these kind of plugins generally works with regex from inside.

My solution for this could be:

(EDITED AND TESTED)

Adding a div for a message

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

the 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>

Hope this helps.

Regards.




回答2:


I can suggest additional solution. I've modified a bit Oscar's solution. I've used Josh Bush's masked input plugin. This plugin was developed ONLY to lead user's input without any validation. I've combined leading with validation. Here my code:

<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>

Of course, this is not a comprehensive client-side date validation (there is no ability here to validate such date as 30/02/2013, for example). I have to use for that server-side date validation.

But nevertheless, this solution minifies client's mistakes.



来源:https://stackoverflow.com/questions/9100526/masked-input-plugin-with-date-limit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!