Jquery datepicker and custom validation

I need to add a special check (I think) to check input from the user. Here is my use case: I am using jquery ui datepicker to select dates with localization as follows:

$.datepicker.setDefaults( $.datepicker.regional[ currentLocale ] );

I use the bassist checker plugin and use the rules for date:true , and therefore does not work with different formats (or, if they do, tell me how!). So I made my own date validation methods like this

  $.validator.addMethod("validDate", function(value) { return parseDateString(value) != null; }, jQuery.validator.messages.date); 

All this works very well, except that when you select a date in datepicker, the check is performed before the component value changes! Therefore, I actually confirm the previous value ....

Does anyone have a solution for me? Thanks in advance:)

+4
source share
1 answer

You can run the check when closing the user datepicker popup:

 $("#birthdate").datepicker({ onClose: function() { /* Validate a specific element: */ $("form").validate().element("#birthdate"); } }); 

Using validate element() function allows you to check a specific field right away.

I created an example here in which any date whose year! = 2011 will cause the verification to fail.

+8
source

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


All Articles