How to analyze if a string is a valid date. I tried everything on the Internet and nothing works. I know that the question is asked several times earlier, but even in threads that say they have solved the problem, does not work for me. I begin to ask myself if there is any way. I will send my code here. I have three text fields: one for a day, one for a month and a year, and to send the form I need to check if they form a valid date.
<html> <head> <title>Simple Form Validation</title> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script> <script type="text/javascript"> jQuery.validator.addMethod( "customDateValidator", function(value, element) { var concateDate; concateDate= $("#day").val() + "/" + $("#month").val() + "/" + $("#year").val(); var rslt= (Date.parseExact(concateDate, "d/M/yyyy") ); if(isNaN(rslt)) return false; else return true; }, "Please enter a valid date" ); </script> <script type="text/javascript"> $(document).ready(function() { $("#form1").validate({ rules: { year: { customDateValidator: true } }, messages: { year: "Please enter valid datum." } }); }); </script> </head> <body> <form id="form1" method="post" action=""> <div class="form-row"><span class="label">Day *</span><input type="text" name="day" id="day" /></div> <div class="form-row"><span class="label">Month *</span><input type="text" name="month" id="month" /></div> <div class="form-row"><span class="label">Year</span><input type="text" name="year" id="year" /></div> <div class="form-row"><input class="submit" type="submit" value="Submit"></div> </form> </body> </html>
source share