Search
  • kriSHna

Validate date format in dynamics using JavaScript code


function testLoad(executionContext){ var formContext = executionContext.getFormContext(); debugger; var datetest = formContext.getAttribute('crb8f_falltestdate').getValue(); formContext.getAttribute('crb8f_falltestdate').setValue(datetest); var test = validatedate(datetest); function validatedate(inputText) { var dateformat = /^(0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])[\-]\d{4}$/; console.log(inputText); // Match the date format through regular expression if(dateformat.test(inputText)) { //document.form1.text1.focus(); //Test which seperator is used '/' or '-' var opera1 = inputText.split('/'); var opera2 = inputText.split('-'); lopera1 = opera1.length; lopera2 = opera2.length; // Extract the string into month, date and year if (lopera1>1) { var pdate = inputText.split('/'); } else if (lopera2>1) { var pdate = inputText.split('-'); } var mm = parseInt(pdate[0]); var dd = parseInt(pdate[1]); var yy = parseInt(pdate[2]); // Create list of days of a month [assume there is no leap year by default] var ListofDays = [31,28,31,30,31,30,31,31,30,31,30,31]; if (mm==1 || mm>2) { if (dd>ListofDays[mm-1]) { //alert('Invalid date format!'); return false; } } if (mm==2) { var lyear = false; if ( (!(yy % 4) && yy % 100) || !(yy % 400)) { lyear = true; } if ((lyear==false) && (dd>=29)) { //alert('Invalid date format!'); return false; } if ((lyear==true) && (dd>29)) { //alert('Invalid date format!'); return false; } } } else { //alert("Invalid date format!"); //document.form1.text1.focus(); return false; } } if(test == false) { formContext.getAttribute('crb8f_falltestdate').setValue(null); formContext.ui.setFormNotification('Please enter a valid date in format MM-DD-YYYY', 'ERROR', 'notification_unique_id'); window.setTimeout(function () { formContext.ui.clearFormNotification('notification_unique_id'); }, 2000); } }



5 views0 comments