validation - jQuery-validate custom rules cause other invalid fields to be ignored -


apologies, it's silly can't work out.

i using c#, asp.net mvc 5 , have following in model:

    /// <summary>     /// none-nullable datetime representing start of meeting.     /// </summary>     [display(name = "date")]     [uihint("date")]     public system.datetime start { get; set; }      /// <summary>     /// time aspect of meeting start split off date     /// portion. property allows correctly display separate     /// date , time uses same underlying property.     /// </summary>     [display(name = "time")]     [uihint("time")]     public system.datetime starttime     {                 {             return start;         }         set         {             start = system.datetime.parseexact(start.tostring("dd/mm/yyyy ") + value.tostring("hh:mm"), "dd/mm/yyyy hh:mm", system.globalization.cultureinfo.invariantculture);          }     }      /// <summary>     /// end time of meeting.     /// since system not allow meetings span multiple days     /// married date part of start when save.     /// </summary>     [display(name = "planned finish")]     [uihint("time")]     public system.datetime finish { get; set; }      /// <summary>     /// set after meeting display actual start time.     /// </summary>     [display(name = "started")]     [uihint("time")]     public system.datetime started { get; set; }      /// <summary>     /// set after meeting display actual finished time.     /// </summary>     [display(name = "finished")]     [uihint("time")]     public system.datetime finished { get; set; }      [required]     [display(name ="primary contact")]     public string contact { get; set; }      [required]     [display(name = "secondary contact")]     public string contact2 { get; set; } 

in browser works fine both contacts (i.e. form doesn't submit unless both have value.) i'm including both jquery-validate.js library , microsoft jquery-validate-unobtrusive.js these seem working fine.

part of problem date , time pickers (noting uihint attributes) use custom editor template creates devexpress date time picker (for convenience later have added 2 classes depending on whether picker date or time input (devexpress-date devexpress-time). works okay, , validation properties such required etc. come through fine. but... since considered date , time inputs fail validation based on format (taking date example accepts format yyyy-mm-dd. i've got around issue following example here: custom date format jquery validation plugin. , have made similar 1 times.

i have laid out in separate js file include straight after both of validation libraries (using bundle). seems okay conceptually (i got guidance from: http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2). but... here things bit tricky.

for clarification customvalidators.js contains:

(function ($) { $.validator.addmethod("date", function (value, element, params) {     if (!this.optional(element)) {         var bits = value.match(/([0-9]+)/gi), str;         if (!bits)             return this.optional(element) || false;         str = bits[1] + '/' + bits[0] + '/' + bits[2];         alert("testing date " + value);         return this.optional(element) || !/invalid|nan/.test(new date(str));     }     return true; }); $.validator.unobtrusive.adapters.addbool("date");  $.validator.addmethod('time', function (value, element) {     value = value.trim();     alert("testing time: " + value);     if (value.length != 5 || value.indexof(":") == -1) {         return false;     }     var parts = value.split(":");     var hour = parseint(parts[0]);     var minutes = parseint(parts[1]);     if (isnan(hour) || isnan(minutes)) {         return false;     }     if (hour < 0 || hour > 23) {         return false;     }     if (minutes < 0 || minutes > 59) {         return false;     }     return this.optional(element) || true; });  $.validator.unobtrusive.adapters.addbool("time"); 

}(jquery));

** problem **

because devexpress inputs register datetimes tries validate using standard datetime rules (returning false , giving me lovely validation summary warning invalid dates).

to around have tried on $(document).ready() bind inputs devexpress-date , devexpress-time properties have different rules:

        $(document).ready(function () {         if ($("form").length > 0)         {             $("form").each(function () {                 $(this).validate();                 $(".devexpress-date input").each(function () {                     $(this).addclass("devexpress-date");                     $(this).rules("add", "date");                 });             });              $("form").each(function () {                 $(this).validate();                 $(".devexpress-time input").each(function () {                     $(this).addclass("devexpress-time");                     $(this).rules("add", "time");                     $(this).rules("remove", "date");                 });             });           }         } 
  • sorry, believe had source adding rules individual inputs can't find *

you'll notice alerts in validator functions, when try submit form get:

trying date: 08/04/2017
trying time: 00:00

i don't alert other 3 time boxes on page , if contact or contact2 fields blank form submits fine. it's i've broken validation after these elements don't understand why.

please can explain me why? application have numbers, text, date , time inputs similar rules ideal solution make custom rules work can run aforementioned rules adding in document.ready() on page has form , automatically work. thought close fact other validations don't work makes me feel i'm long way off intention, appreciated.

thanks.

i ran version 1.14. upgrading jquery.validate.js 1.17 , jquery.validate.unobtrusive.js 3.2.6 fixed problem.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -