Masked input plugin with date limit

I am using masked input in an input text box for a date in MM / YYYY format. ( http://digitalbush.com/projects/masked-input-plugin/ )

My javascript code is:

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

And the HTML code:

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

When I start to write something, it takes the value 12/2099 in the field, and it is impossible to write another thing.

If I write 99/9999 in a javascript mask, users can write a date, which is a lie ... so I'm not doing what I need to do?

I want users to be able to write from 01/1990 to 12/2099, perhaps adding to the restriction with a date value of + 20 years or something like that ...

+3
source share
2 answers

, , , , .

:

( )

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>

, .

.

+5

. Oscar. - . - . . :

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

, ( ​​, , 30/02/2013). .

, , .

+5

Source: https://habr.com/ru/post/1535511/


All Articles