Introduction
I have read a lot of articles on time validation in a web based application, but could not get my kind of solution. My requirement was to write any string as time and the program should be intelligent enough to validate it. For instance, if the user type 1:4 5 AM (which should be 1:45 AM), the code should validate and correct it by removing the additional space. Not only that, my sample application checks for valid hour, minute, and AM/PM as well, and takes time format as (HH:MM AM/PM).
Code Explanation
Create a sample ASP.NET application and add a .NET TextBox
control and a client-side button. The OnClick
of the button will call the validatetime()
method.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type="button" value="validate time" onclick="validatetime()">
The validatetime()
JavaScript method will internally call two other functions: trimAllSpace()
and trimString()
.
function validatetime()
{
var strval = document.Form1.TextBox1.value;
var strval1;
if(strval.length < 6)
{
alert("Invalid time. Time format should be HH:MM AM/PM.");
return false;
}
if(strval.lenght > 8)
{
alert("invalid time. Time format should be HH:MM AM/PM.");
return false;
}
strval = trimAllSpace(strval);
if(strval.charAt(strval.length - 1) != "M" && strval.charAt(
strval.length - 1) != "m")
{
alert("Invalid time. Time shoule be end with AM or PM.");
return false;
}
else if(strval.charAt(strval.length - 2) != 'A' && strval.charAt(
strval.length - 2) != 'a' && strval.charAt(
strval.length - 2) != 'p' && strval.charAt(strval.length - 2) != 'P')
{
alert("Invalid time. Time shoule be end with AM or PM.");
return false;
}
strval1 = strval.substring(0,strval.length - 2);
strval1 = strval1 + ' ' + strval.substring(strval.length - 2,strval.length)
strval = strval1;
var pos1 = strval.indexOf(':');
document.Form1.TextBox1.value = strval;
if(pos1 < 0 )
{
alert("invlalid time. A color(:) is missing between hour and minute.");
return false;
}
else if(pos1 > 2 || pos1 < 1)
{
alert("invalid time. Time format should be HH:MM AM/PM.");
return false;
}
var horval = trimString(strval.substring(0,pos1));
if(horval == -100)
{
alert("Invalid time. Hour should contain only integer value (0-11).");
return false;
}
if(horval > 12)
{
alert("invalid time. Hour can not be greater that 12.");
return false;
}
else if(horval < 0)
{
alert("Invalid time. Hour can not be hours less than 0.");
return false;
}
var minval = trimString(strval.substring(pos1+1,pos1 + 3));
if(minval == -100)
{
alert("Invalid time. Minute should have only integer value (0-59).");
return false;
}
if(minval > 59)
{
alert("Invalid time. Minute can not be more than 59.");
return false;
}
else if(minval < 0)
{
alert("Invalid time. Minute can not be less than 0.");
return false;
}
minpos = pos1 + minval.length + 1;
if(strval.charAt(minpos) != ' ')
{
alert("Invalid time. Space missing after minute.
Time should have HH:MM AM/PM format.");
return false;
}
return true;
}
Here is the trimAllSpace()
function:
function trimAllSpace(str)
{
var str1 = '';
var i = 0;
while(i != str.length)
{
if(str.charAt(i) != ' ')
str1 = str1 + str.charAt(i); i ++;
}
return str1;
}
Here is the trimString()
function:
function trimString(str)
{
var str1 = '';
var i = 0;
while ( i != str.length)
{
if(str.charAt(i) != ' ') str1 = str1 + str.charAt(i); i++;
}
var retval = IsNumeric(str1);
if(retval == false)
return -100;
else
return str1;
}
The IsNumeric()
function will check whether the user has entered a numeric value or not.
function IsNumeric(strString)
{
var strValidChars = "0123456789";
var strChar;
var blnResult = true;
if (strString.length == 0)
return false;
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}
That is all. There is nothing much to explain about the above code blocks. Every line of code is really simple.
Working with Logica Pvt. Ltd as a Tech Lead.