Monday, July 7, 2008

Time Validation JS

//Time Validation
/* This function validate time in the format of HH:MM:SS AP/PM. The argument timeValue is the id of the textbox.
The argument required defines whether this field is either optionsl or not. It allows time in two formats.
One is Railway time format(does not contain AM or PM) and another is normal. */
function validateTime(timeValue, required)
{
var timeStr = timeValue.value;
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AMamPMpm))?$/;
if(required == true)
{
if(timeStr == '' timeStr == "" timeStr == null)
{
alert("Enter the time");
timeValue.select();
timeValue.focus();
return false;
}
else
{
if(timeStr.match(timePat))
{
var s = isValidTime(timeStr, timeValue)
return s;
}
else
{
alert("Enter the time in the correct format");
timeValue.select();
timeValue.focus();
return false;
}
}
}
else
{
if(timeStr == '' timeStr == "" timeStr == null)
{
return true;
}
else
{
if(timeStr.match(timePat))
{
var s = isValidTime(timeStr, timeValue)
return s;
}
else
{
alert("Enter the time in the correct format");
timeValue.select();
timeValue.focus();
return false;
}
}
}
}

No comments: