Jump to content

Recommended Posts

Hello,I need the help on the following in order to validate date on my form (validation is onBlur): - validation on form date format: dd-mm-yyyy , when I go to next field, if the date field is empty, it will be filled with today's date, and I write 9-3 and go to next field it will automatically write 9-3-2013 (where is 2013 is the current year), finally, if the date is not valid then user can't go to another field, focus must be go back to date field. Help is much appreciated. Thanks in advance

Share this post


Link to post
Share on other sites

There are several steps there, what do you have so far and which steps do you need help with?

Share this post


Link to post
Share on other sites

If the field is empty then you can create a Date object and get the current date parts from it. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date For auto-complete, you can split the date string up on the "-" character, and if there are less than 3 elements in the array then you can add the year. For validating the date, there is example code here: http://www.javascriptkit.com/script/script2/validatedate.shtml It expects a different date format then you're looking for, but you can modify that code to expect the format that you want.

Share this post


Link to post
Share on other sites

Here it is, problem now, is that when I write 4-5 or 4-5- I need to autocomple the year with the current year: var dtCh= "-";var minYear=1900;var maxYear=2100;function isInteger(s){ var i; for (i = 0; i < s.length; i++){ // Check that current character is number. var c = s.charAt(i); if (((c < "0") || (c > "9"))) return false; } // All characters are numbers. return true;}function stripCharsInBag(s, bag){ var i; var returnString = ""; // Search through string's characters one by one. // If character is not in bag, append to returnString. for (i = 0; i < s.length; i++){ var c = s.charAt(i); if (bag.indexOf© == -1) returnString += c; } return returnString;}function daysInFebruary (year){ // February has 29 days in any year evenly divisible by four, // EXCEPT for centurial years which are not also divisible by 400. return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}function DaysArray(n) { for (var i = 1; i <= n; i++) { this = 31 if (i==4 || i==6 || i==9 || i==11) {this = 30} if (i==2) {this = 29} } return this}function isDate(dtStr){ var daysInMonth = DaysArray(12) var pos1=dtStr.indexOf(dtCh) var pos2=dtStr.indexOf(dtCh,pos1+1) var strDay=dtStr.substring(0,pos1) var strMonth=dtStr.substring(pos1+1,pos2) var strYear=dtStr.substring(pos2+1) strYr=strYear if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1) if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1) for (var i = 1; i <= 3; i++) { if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1) } month=parseInt(strMonth) day=parseInt(strDay) year=parseInt(strYr) if (pos1==-1 || pos2==-1){ alert("The date format should be : dd-mm-yyyy") return false } if (strMonth.length<1 || month<1 || month>12){ alert("Please enter a valid month") return false } if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){ alert("Please enter a valid day") return false } if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){ alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear) return false } if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){ alert("Please enter a valid date") return false }return true}function ValidateForm(){ var dt=document.getElementById("ADate");var currentTime4 = new Date()var month4 = currentTime4.getMonth() + 1var day4 = currentTime4.getDate()var year4 = currentTime4.getFullYear()var finalDate4 = ( day4 + "-" + month4 + "-" + year4 );if(dt.value == ''){ dt.value = finalDate4; }if (isDate(dt.value)==false) { dt.focus() return false } return true}/*function ValidateForm(){ var dt=document.frmSample.txtDate if (isDate(dt.value)==false){ dt.focus() return false } return true }*/

Share this post


Link to post
Share on other sites

There's a lot of code there to do things that shouldn't take so much. Like this code:

var pos1=dtStr.indexOf(dtCh)var pos2=dtStr.indexOf(dtCh,pos1+1)var strDay=dtStr.substring(0,pos1)var strMonth=dtStr.substring(pos1+1,pos2)var strYear=dtStr.substring(pos2+1)strYr=strYearif (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)for (var i = 1; i <= 3; i++) {if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)}month=parseInt(strMonth)day=parseInt(strDay)year=parseInt(strYr)if (pos1==-1 || pos2==-1){alert("The date format should be : dd-mm-yyyy")return false}

I suggested splitting the date string. This is probably easier to understand:

var dateParts = dtStr.split(dtCh); if (dateParts.length < 2){  alert("The date format should be : dd-mm-yyyy");  return false;}var day = parseInt(dateParts[0], 10);var month = parseInt(dateParts[1], 10); var year = 0;if (dateParts.length == 3){  year = parseInt(dateParts[2], 10);}if (isNaN(day) || isNaN(month) || isNaN(year)){  alert("The date parts must be numeric.");  return false;}

Now you haev the month, day, and year as numbers. The year will be 0 if it wasn't given. There is a problem in your logic, though. Your isDate function only returns true or false. Where do you expect the code to go to add the year if it is missing? You can put that code in isDate to add the year and update the value in the field, but that seems like a weird thing to do in a function that just validates a date. Then you have all of this code to check the ranges:

if (strMonth.length<1 || month<1 || month>12){alert("Please enter a valid month")return false}if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){alert("Please enter a valid day")return false}if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)return false}if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){alert("Please enter a valid date")return false}

You only need to do this, like the second link I posted above shows: var dayobj = new Date(year, month-1, day);if ((dayobj.getMonth()+1!=month)||(dayobj.getDate()!=day)||(dayobj.getFullYear()!=year))alert("Invalid Day, Month, or Year range detected. Please correct and submit again.");

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...