Focal Point
[CLOSED] Date Validation in Maintain Read/Write Grid.

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/6337091916

April 19, 2012, 12:51 AM
Shankar
[CLOSED] Date Validation in Maintain Read/Write Grid.
Hi,
I have a Read/Write Grid in maintain in which there are few updatable columns which takes Date values from user in YYYY/MM/DD format to update the datasource.I want to do validation whether user enters correct value or not.I am using below piece of code to restrict user to enter value in correct format.
 
function OnGrid1_OnCellChange ( )  {
ClientRef.Grid1.UseMaskedEdit(1); // enable mask editing
ClientRef.Grid1.QuickSetMask (2, newRow, "0000/00/00");
}

function OnGrid1_OnEditFinish ( )  {
if ((col == 2) && (text.length != 10))
{
alert('Date can only be in YYYY/MM/DD format.');
}
}


But user can enter any incorrect value like '3333/33/33' and it will pass this validation but ideally its not a valid date.Is there any way to keep these things under check that user is entering valid date? What kind of validation is required to handle this? Please suggest.

Thanks in advance.

This message has been edited. Last edited by: Kerry,


WF 8.1.04,Windows 7,
DataBase: Oracle 11g,Output :Excel,PDF,HTML
April 19, 2012, 09:09 AM
JRLewis
I have used a date validation routine that looks like this:

function CheckDate(date) {
 
   valid = true;
   var alertmsg = '' ;
 
   <!-- ensure in mm/dd/yyyy format -->
   if ((date.length != 10) || (date.substr(2,1) != '/') || (date.substr(5,1) != '/'))
      {
       valid = false;
       alertmsg = 'Invalid Date - format must be mm/dd/yyyy';
      }
 
  <!-- ensure a valid date -->
   if (valid)
      {
       var MM   = date.substr(0,2);
       var DD   = date.substr(3,2);
       var YYYY = date.substr(6,4);
       valid    = IsDate(YYYY, MM, DD);
       if (valid)
          {
           <!-- do nothing -->
          }
       else
          {
           alertmsg = 'Invalid Date - format must be mm/dd/yyyy';
          }
      }
 
  <!-- ensure not future date -->
   if (valid)
      {
      <!-- get today's date and convert to milliseconds using getTime to compare to newly selected date -->
       var Today      = new Date();
       var TodayCheck = Today.getTime();
 
      <!-- convert date to milliseconds using getTime to compare to today's date -->
       var NewDate      = new Date(YYYY, MM - 1, DD);
       var NewDateCheck = NewDate.getTime();
 
      <!-- check for a future date -->
       if (NewDateCheck > TodayCheck)
          {
           alertmsg = 'Invalid Date - date selected is in the future';
           valid    = false;
          }
      }
 
    return valid;
}
 
function IsDate(year, month, day) {
 
<!-- check for valid dates, javascript months are 0 through 11 -->
   month = month - 1;
 
   var calcDate = new Date(year,month,day);
 
   if ((year  == calcDate.getFullYear()) &&
       (month == calcDate.getMonth()) &&
       (day   == calcDate.getDate()))
        return true;
   else
        return false;
}




WebFOCUS 8
April 19, 2012, 02:09 PM
Alan B
I use regular expressions in javascript for this:

Try this out:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Validate dd/mm/yyy date entry</title>
<script type="text/javascript">
function validDate(fld) {
    var wx=document.getElementById(fld.id);
    var RegExPattern = /^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/;
    var errorMessage = 'Please enter valid date as four digit year, month and day.\nYou may use a slash, a hyphen or a period to separate the date parts.';
    if ((wx.value.match(RegExPattern)) && (wx.value!='')) {
        return true;
    } else {
        alert(errorMessage);
    }
}
</script>
</head>
<body>
<form>
<p><input type="text" name="dateEntry" id="dateEntry" onchange="javascript:validDate(this);">
<br>(yyyy/mm/dd)
</form>
</body>
</html>



Alan.
WF 7.705/8.007