Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Date Validation script

Avatar

Level 2

Is there a script that I can use that would give an error mesg when a user does not enter data in a (MM/DD/YYYY) format?  The field is a text field.  I have tried using the Validation Pattern and did not have any success.

Thanks for the time and help.

7 Replies

Avatar

Former Community Member

This should do the trick, where 'foo' is a text field.

// form1.page1.foo::exit - (JavaScript, client)

var dateStr = this.rawValue;

var regExp = /^\d{1,2}\/\d{1,2}\/d{4}$/;

if (!regExp.test(dateStr)) {

xfa.host.messageBox("Please enter the date in the format MM/DD/YYYY.");

}

This could be extended to validate the range of months, range of days, days in each month, etcetera.
Steve

Avatar

Level 7

Steve, how I cam modify the script for date format like: March 9, 2010

Thanks

Avatar

Former Community Member

Are you referring to the regular expression required to validate the display pattern MMMM D, YYYY?

Steve

Avatar

Level 7

Hi Steve, I am sorry I do not know what you mean Regular expression!

Just we like users for the form to use the same date format(example: March 10, 2009) to avoind wrong inputs.

There is any way if they do not select from calander the date field stay blank or any suggestion?

Thanks Steve

Avatar

Former Community Member

A regular expression is a pattern matching tool that can be used for validating form fields.

For example, to match the date format MM\DD\YYYY I used var regExp = /^\d{1,2}\/\d{1,2}\/d{4}$/;

If you create a form with a Date/Time field and add the pattern 'date{MMMM D, YYYY}' as both the display and validation pattern, you can control the date format as March 10, 2010, for example.

Steve

Avatar

Level 7

At list is  it possible when the user types into date field and the date it's not the correct format

as date{MMMM D, YYYY} the field to remain empty and with a pop up message to force them to use the calander?

Thanks

Avatar

Level 7

Searching around I think I found a solution and I will like to share with you.

On Exit Event apply the script:

form1.P1.DateTimeField1::exit - (JavaScript, client)

if

(this.rawValue == this.formattedValue)

{

app.alert("Please use the calandar or enter a date in 'MM/DD/YY' format");

xfa.host.setFocus(this.somExpression);

this.rawValue

="";

}

For the date field my Display Pattern is:date{MMMM D, YYYY}

And the Edit Pattern is: date{MM/DD/YY}|date{MM-DD-YY}|date{MM-DD-YYYY}|date{MM/DD/YYYY}

Thanks