Hi,
This is a perfect application for using regular expressions.
What you want to do is:
- Ensure that the year is specified in numeric format (all digits). You'll probably only want years from 1900 to 2099, but you can make this broader if need be.
- The month can only be 01-09, 10, 11 or 12. Anything less than 01 and greater than 12 is an invalid month. Single digits for a month (no leading zero) are also invalid (e.g. 1 instead of 01).
You could write all this using substrings and validating the first 4 characters as the year and the last 2 characters as the month. But I suggest using a regular expression to do this.
Here's a snippet of the regular expression code you could put in FieldEdit PeopleCode for your character data field:
Local JavaObject &jCheckDateRegex;
Local JavaObject &jInput;
&jCheckDateRegex = CreateJavaObject("java.lang.String", "(19|20)\d\d0[1-9]|1[012]");
&jInput = CreateJavaObject("java.lang.String", YOUR_RECORD.YOUR_FIELD);
If &jInput.matches(&jCheckDateRegex) Then
MessageBox(0, "", 0, 0, "Input is valid");
Else
MessageBox(0, "", 0, 0, "Invalid input. Please use the format YYYYMM with a year between 1900 to 2099");
End-If;
The key to this code is the regular expression: (19|20)\d\d0[1-9]|1[012]
I based this on a dates example in the regular expressions tutorial.
If you wanted to allow any year you could simply use \d\d\d\d0[1-9]|1[012] (Note that each \d matches any decimal 0-9)
The 0[1-9] means that a single digit month must have a zero in front of it (so January needs to be specified as 01 not 1). Alternatively (the pipe character | is an OR), the month can consist of the number 1 followed by a 0, 1 or 2 (i.e. 10, 11 or 12). So a month of 13 or 99 would be invalid.
If you want to play around with the regular expression more, I suggest using this free online RegEx Tester.