These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Date Field Validation - Java Regular Expression

Fridgie
Registered: Mar 24 2009
Posts: 26

Hi everyone, I hope someone can help me on this. I've created the following date validation script for a date/time field that is set to run in a dynamic form. The issue I am having however is that it validates everything except whether or not the "/" is used as part of the date entry ie 28/02/2008. What I am trying to avoid is having the user enter a date using another type of symbol ie 28-02-2008. Any thoughts on how I should change my Regular Expression to address this issue?
(Please note I wish to keep the date validation as a java script using a regular express - to keep things focused I do realize there other ways of date validation). Thank you.

var myRegExp = /\d?\d\W\d?\d\W\d?\d?\d\d/;
var myDate = (this.rawValue);
var DD = parseFloat(this.formattedValue.substr(0,2));
var MM = parseFloat(this.formattedValue.substr(3,2));
var YYYY = parseFloat(this.formattedValue.substr(6,4));
var msg = "DATE FORMAT ERROR"

if (myDate == null);

else if(myRegExp.test(myDate) == true);

else if(myRegExp.test(myDate) == false && this.formattedValue.length == 10)
{app.alert(msg); this.rawValue = null}

if(DD > 31 || DD < 1)
{app.alert(msg); this.rawValue = null};

if (MM > 12 || MM < 1)
{app.alert(msg); this.rawValue = null};

if (YYYY > 2100 || YYYY < 1850)
{app.alert(msg); this.rawValue = null};

if (this.formattedValue.length != 0 && this.formattedValue.length != 10)
{app.alert(msg); this.rawValue = null};

My Product Information:
LiveCycle Designer, Windows
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
Instead of using "\W" replace it with "\/" (no quotes). That will force a forward slash to be used as the separator.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Fridgie
Registered: Mar 24 2009
Posts: 26
Thank you for your reply. Based on your suggestion, I would change as follows:

var myRegExp = /\d?\d\V\d?\d\V\d?\d?\d\d/;

Is this correct. Making this change brings up my date format error msg if I enter the date in the format 28/02/2008 or use the drop down box from the calendar to select a date. Is there something I'm doing wrong? Thanks
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
It's not the letter "V" but a back-slash followed by a forward-slash. You should copy-paste what I wrote before.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Fridgie
Registered: Mar 24 2009
Posts: 26
Thanks for your reply try67. Your suggestion has worked for the most part.
Correct me if I'm wrong but the change would be as follows:

var myRegExp = /\d?\d\/\d?\d\/\d?\d?\d\d/;

This works for manual entry of the date, which is great. I notice however that if someone chooses a date from the drop down calendar box it comes back with an error. Any idea why this would be and how I could get round it (other than getting rid of the drop down box)? Thanks
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
Don't know... which values do you have in it?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com