The following Java Script use to validate date format for web client.
Date validation in web client.
The
following Java Script use to validate date format for web client.
JS Header, The script have to be place in JS Header. The gc_DateFormat is global variable use to select date format
to be "DD/MM/YYYY" or "MM/DD/YYYY" . There are 2 functions in JS Header, CheckDate(dateV,vl_Label,vl_ManField)
is called by onSubmit event, it check the passing
field have value or not if it have then call function isValidDate(dateV,DateStr, vl_Lable) to validate date format. The isValidDate(dateV,dateStr,vl_Lable)
is function use to compare Object 'dateV' and string
'dateStr' if the value of dateStr
does not match with dateV then It return error
message and false.
You can specify
mandatory field by passing 'true' or 'false' to vl_ManField
and pass label name to field vl_Lable for showing field error message.
/* Passing parameter
- dateV is date field .
- vl_Lable is label name for the
field.
- vl_ManField is used to check for
mandatory field or not,
if it is true the field is madatory
else false
Global variable using.
-
gc_DateFormat is used to
select date format between 'DD/MM/YYYY' or
'MM/DD/YYYY'
-gc_Submit is used to keep submit status, when you click
save button this
variable will return value to 'true'
*/
var gc_DateFormat
= "DDMM" //date
format DD/MM/YYYY
//var
gc_DateFormat = "MMDD" //date
format MM/DD/YYYY
var gc_Submit
= false; //Date
format will validate when click submit button
function
CheckDate(dateV,vl_Label,vl_ManField)
{
var dateStr
= dateV.value;
if(dateStr==""){
//Mandatory
field checking
if (! vl_ManField) {
return true
}
else{
alert("You must enter " + vl_Label
+ " field");
dateV.focus();
return false;
}
}
if ( isValidDate(dateV, dateStr,vl_Label) ==
false)
return false
else
return true
}
function
isValidDate(dateV, dateStr, vl_Label) {
//
Checks for the following valid date formats:
//
MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
//
Also separates date into month, day, and year
variables
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
//
To require a 4 digit year entry, use this line
instead:
//
var datePat =
/^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray
= dateStr.match(datePat);
// is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format for " + vl_Label + " field");
dateV.focus();
return false;
}
if (gc_DateFormat ==
"MMDD" ) {
month = matchArray[1]; // parse
date into variables
day = matchArray[3];
}
else {
month = matchArray[3]; // parse
date into variables
day = matchArray[1];
}
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12 for "+ vl_Label +" field");
dateV.focus();
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31 for" + vl_Label +" field");
dateV.focus();
return false;
}
if ((month==4 || month==6 || month==9 || month==11)
&& day==31) {
alert("Month "+month+" doesn't have 31 days!
for "+ vl_Label +" field");
dateV.focus();
return flase;
}
if (month == 2) { // check for february
29th
var isleap
= (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap))
{
alert("February " + year + " doesn't have
" + day + " days! for " + vl_Label +" field");
dateV.focus();
return false;
}
}
return true;
}
onSubmit event, The onSubmit event call function CheckDate(dateV,vl_Label,vl_ManField) to validate date field when
user click submit button. When you call this function you have to pass date
field name, label name and 'true' for mandatory filed, 'false' for non
mandatory field. The following is example for date validatation, it have to be place in onSubmit
event.
/* Validation Date format for each
date fields.
Pass field name, label
name and 'true' for mandatory field,
'flase' to non mandatory field
*/
if(gc_Submit){
gc_Submit = false;
if (! CheckDate(fl_dRestDt,"'Date of restructuring'",false)){
return false
}
if (! CheckDate(fl_dDefDate,"'Date of Default'",false)){
return false
}
if (! CheckDate(fl_dBkGdeDt,"'Date of grading'",false)){
return false
}
if (! CheckDate(fl_dOurGdeDt,"'Date of grading'",false)){
return false
}
return true
}
onClick event. The following
JS script have to place in onClick event of the
'Save' button, select run on Web instead of Client and select programming
language as JavaScript.
/* Set global variable gc_Submit to 'true' for triggering onSumit
event and
call form.sumit method to perform ducument save */
gc_Submit
= true;
if
(document.forms[0].onsubmit())
{
document.forms[0].submit();
}
It is better to copy all of java script in JS header to subform, then include subform to
working form. The subform must have $$Return field
name, field type should be Text and Computed for display and value have to be
as the following formula.
tURL:="/" + @WebDbName + "/ve_AllDocs/"
+ @Text( @DocumentUniqueID ) + "?Opendocument";
"[" + tURL + "]"
Notes: If Save button
is used as share button, it will effect to all form that use this share button.
It need to have $$Return field including code as above
example and onSubmit event have to write code
"return true".