Expand my Community achievements bar.

SOLVED

Date Field validation

Avatar

Former Community Member

Hi All,

I have a simple date field which is of pattern(DD/MM/YYYY).

I have to validate the date in such a way that it should be less than or equal to the current date.

If the user enters a date greater than the current date it should give a proper message and the value of the date field should become null.

Pls help.

Thanks

Abhiram

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Abhiram,

The date will be in the correct format when the formattedValue is not the same as the rawValue.  The rawValue will always be in yyyy-mm-dd format, except when the value fails the date pattern.  It will also be a String type not a Date so you will need to convert it to a Date to do any calculation on it.  Try something like the following in the validate event.

if (DateTimeField1.rawValue !== null)
{
if (DateTimeField1.formattedValue == DateTimeField1.rawValue)
{
  xfa.host.messageBox("Please enter a date in DD/MM/YYYY format");
}
else
{
  var d = util.scand("yyyy-mm-dd", DateTimeField1.rawValue);
  if (d > Date.now())
  {
   xfa.host.messageBox("Please enter a date on or before today");
   DateTimeField1.rawValue = null;
  }
}
}

Bruce

View solution in original post

6 Replies

Avatar

Correct answer by
Level 10

Hi Abhiram,

The date will be in the correct format when the formattedValue is not the same as the rawValue.  The rawValue will always be in yyyy-mm-dd format, except when the value fails the date pattern.  It will also be a String type not a Date so you will need to convert it to a Date to do any calculation on it.  Try something like the following in the validate event.

if (DateTimeField1.rawValue !== null)
{
if (DateTimeField1.formattedValue == DateTimeField1.rawValue)
{
  xfa.host.messageBox("Please enter a date in DD/MM/YYYY format");
}
else
{
  var d = util.scand("yyyy-mm-dd", DateTimeField1.rawValue);
  if (d > Date.now())
  {
   xfa.host.messageBox("Please enter a date on or before today");
   DateTimeField1.rawValue = null;
  }
}
}

Bruce

Avatar

Level 2

Hi Bruce

I noticed your post. I  am attempting the same thing only not using a DateField but a regular textfield (since I want the user to enter the date manually and not be able to pop up the calendar)   with the option of entering dates in the future only its not working!??

I did 2 changes to the code as follows:

Firstly:  The line  if(T1.fomattedValue == T1.rawValue)  ====>>   if (T1.formattedValue !== T1.rawValue)

Secondly I changed the if(d>Date.now()) ====>  if(d<Date.now)

So what am i missing???

Looking forward to your response

Meir R.

Avatar

Level 10

With a text field I would do something like;

if (TextField1.rawValue !== null)
{
  var d = util.scand("dd/mm/yyyy", TextField1.rawValue);
  if (d === null)
  {
    xfa.host.messageBox("Please enter a date in DD/MM/YYYY format");
  }
  else
  {
    if (d < Date.now())
    {
     xfa.host.messageBox("Please enter a date in the future");
    }
  }
}

I think you will validate the date by testing for a null return from util.scand.

Bruce

Avatar

Level 2

Bruce Hi

It works like a charm!!!

Thanks a million...

Meir

Avatar

Former Community Member

Bruce,

The code is working fine in the validate event of the date field but when I m trying to make a script fragment of the same code it's not giving the desired result.

Below is the code I used for the fragment.

function CurrentDateValidator(dateEntered)

{

if (dateEntered!== null)

{

var d = util.scand("yyyy-mm-dd", dateEntered);

if (d > Date.now())

{

xfa.host.messageBox("Please enter a date on or before today");

return null;

}

}

}

I m using an action to call the function CurrentDateValidator(xfa.event.newText) and assigning the result to the same date field.

Pls help.

Thanks

Abhiram