<input name="txtCalander"
type="text" style="color: red">id="txtCalander"
maxlength="10"
title="MM/DD/YYYY"style="mso-spacerun: yes">
onblur="javascript:ValidDate(this,event,'/');"
onkeyup="
AutoDateFormat(this,event,'/');"
style="width: 132px"
/>
/* Function isDate is used to verify
if the given value is a valid date in the format (mm/dd/yyyy) : This function first
specifies the non-digit character which is allowed in the date ("/" is used here).
You can also specify the year range between which the date is valid. These changeable
values are declared in the lines (found in the beginning of the code) :
// Declaring valid date
character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;
If the valid date character
is changed please make sure you replace it in the HTML and Javascript code too.
Now the date string
is seperated into the month, day and year integers. They are each validated seperately
and combined together.
* Function ValidateForm is
used to check if the date field is valid on form submission
*/
var
dtCh= "-";
var
minYear=1900;
var
maxYear=2100;
function
isInteger(s){
style="mso-tab-count: 1"> var
i;
style="mso-spacerun: yes"> for
(i = 0; i < s.length; i++)
style="mso-spacerun: yes"> {
style="mso-spacerun: yes">
// Check that current character is number.
style="mso-spacerun: yes">
var c = s.charAt(i);
style="mso-spacerun: yes">
if (((c < "0") (c >
"9")))
style="mso-spacerun: yes"> style="color: blue">return false;
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> // All characters
are numbers.
style="mso-spacerun: yes"> return
true;
}
function
stripCharsInBag(s, bag){
style="mso-tab-count: 1"> var
i;
style="mso-spacerun: yes"> var
returnString = "";
style="mso-spacerun: yes"> // Search
through string's characters one by one.
style="mso-spacerun: yes"> // If character
is not in bag, append to returnString.
style="mso-spacerun: yes"> for
(i = 0; i < s.length; i++)
style="mso-spacerun: yes"> {
style="mso-spacerun: yes">
var c = s.charAt(i);
style="mso-spacerun: yes">
if (bag.indexOf(c) == -1)
style="mso-spacerun: yes"> returnString
+= c;
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> return
returnString;
}
function
daysInFebruary (year)
{
style="mso-tab-count: 1"> // February
has 29 days in any year evenly divisible by four,
style="mso-spacerun: yes"> // EXCEPT
for centurial years which are not also divisible by 400.
style="mso-spacerun: yes"> return
(((year % 4 == 0) && ( (!(year % 100 == 0)) (year % 400 == 0))) ? 29
: 28 );
}
function
DaysArray(n)
{
style="mso-tab-count: 1"> for
(var i = 1; i <= n; i++)
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
this[i] = 31
style="mso-tab-count: 2">
if (i==4 i==6 i==9 i==11)
style="mso-tab-count: 2"> {
style="mso-tab-count: 2">
this[i] = 30
style="mso-tab-count: 2"> }
style="mso-tab-count: 2">
if (i==2)
style="mso-tab-count: 2"> {
style="mso-tab-count: 2">
this[i] = 29
style="mso-tab-count: 2"> }
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> return
this
}
function
isDate(object,Seperater)
{
style="mso-spacerun: yes"> dtCh= Seperater;
style="mso-spacerun: yes"> var
dtStr=object.value;
style="mso-tab-count: 1"> var
daysInMonth = DaysArray(12)
style="mso-tab-count: 1"> var
pos1=dtStr.indexOf(dtCh)
style="mso-tab-count: 1"> var
pos2=dtStr.indexOf(dtCh,pos1+1)
style="mso-tab-count: 1"> var
strMonth=dtStr.substring(0,pos1)
style="mso-tab-count: 1"> var
strDay=dtStr.substring(pos1+1,pos2)
style="mso-tab-count: 1"> var
strYear=dtStr.substring(pos2+1)
style="mso-tab-count: 1"> strYr=strYear
style="mso-tab-count: 1"> if
(strDay.charAt(0)=="0" && strDay.length>1)
strDay=strDay.substring(1)
style="mso-tab-count: 1"> if
(strMonth.charAt(0)=="0" && strMonth.length>1)
strMonth=strMonth.substring(1)
style="mso-tab-count: 1"> for
(var i = 1; i <= 3; i++) {
style="mso-tab-count: 2">
if (strYr.charAt(0)=="0" &&
strYr.length>1) strYr=strYr.substring(1)
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> month=parseInt(strMonth)
style="mso-tab-count: 1"> day=parseInt(strDay)
style="mso-tab-count: 1"> year=parseInt(strYr)
style="mso-tab-count: 1"> if
(pos1==-1 pos2==-1)
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
//alert("The Date Format Should be in MM"+dtCh+"DD"+dtCh+"YYYY");
style="mso-tab-count: 2"> ShowMessage(object,style="color: #a31515">"The Date Format Should be in MM"+dtCh+"DD"+dtCh+style="color: #a31515">"YYYY","RED");
style="mso-tab-count: 2"> document.getElementById(object.id).value
= "";
style="mso-tab-count: 2">
return false;
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> else
style="mso-tab-count: 1"> {
style="mso-tab-count: 1">
ClearMessage(object);
style="mso-tab-count: 1"> }
style="mso-tab-count: 1">
style="mso-tab-count: 1"> if
(strMonth.length<1 month<1 month>12)
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
//alert("Invalid Month")
style="mso-tab-count: 2"> ShowMessage(object,style="color: #a31515">"Invalid Month","RED");
style="mso-tab-count: 2"> document.getElementById(object.id).value
= "";
style="mso-tab-count: 2">
return false
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> else
style="mso-tab-count: 1"> {
style="mso-tab-count: 1">
ClearMessage(object);
style="mso-tab-count: 1"> }
style="mso-tab-count: 1">
style="mso-tab-count: 1"> if
(strDay.length<1 day<1 day>31 (month==2 && day>daysInFebruary(year))
day > daysInMonth[month])
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
//alert("Invalid Day")
style="mso-tab-count: 2"> ShowMessage(object,style="color: #a31515">"Invalid Date","RED");
style="mso-tab-count: 2"> document.getElementById(object.id).value
= "";
style="mso-tab-count: 2">
return false
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> else
style="mso-tab-count: 1"> {
style="mso-tab-count: 1">
ClearMessage(object);
style="mso-tab-count: 1"> }
style="mso-tab-count: 1">
style="mso-tab-count: 1"> if
(strYear.length != 4 year==0 year<minYear year>maxYear)
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
//alert("Enter a Valid 4 Digit Year Between "+minYear+" and "+maxYear)
style="mso-tab-count: 2"> ShowMessage(object,style="color: #a31515">"Enter a Valid 4 Digit Year Between "+minYear+style="color: #a31515">" and "+maxYear,"RED");
style="mso-tab-count: 2"> document.getElementById(object.id).value
= "";
style="mso-tab-count: 2">
return false
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> else
style="mso-tab-count: 1"> {
style="mso-tab-count: 1">
ClearMessage(object);
style="mso-tab-count: 1"> }
style="mso-tab-count: 1">
style="mso-tab-count: 1"> if
(dtStr.indexOf(dtCh,pos2+1)!=-1 isInteger(stripCharsInBag(dtStr, dtCh))==style="color: blue">false)
style="mso-tab-count: 1"> {
style="mso-tab-count: 2">
//alert("Invalid Date")
style="mso-tab-count: 2"> ShowMessage(object,style="color: #a31515">"Invalid Date","RED");
style="mso-tab-count: 2"> document.getElementById(object.id).value
= "";
style="mso-tab-count: 2">
return false
style="mso-tab-count: 1"> }
style="mso-tab-count: 1"> else
style="mso-tab-count: 1"> {
style="mso-tab-count: 1">
ClearMessage(object);
style="mso-tab-count: 1"> }
style="mso-tab-count: 1">
return
true
}
function
ValidDate(object,e,Seperater)//onblur
{
style="mso-spacerun: yes"> if(object.value!=style="color: #a31515">"")
style="mso-spacerun: yes"> {
style="mso-spacerun: yes">
if (isDate(object,Seperater)==false)
style="mso-spacerun: yes"> {
style="mso-spacerun: yes"> CursorFocusSelect(object);
style="mso-tab-count: 2">
}
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> else
style="mso-spacerun: yes"> {
style="mso-spacerun: yes"> ClearMessage(object);
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> }
style="mso-spacerun: yes">
function
AutoDateFormat(object,e,Seperater)//onkeyup
{
style="mso-spacerun: yes"> var
ValLen=object.value.length;
style="mso-spacerun: yes"> var
Val=object.value;
style="mso-spacerun: yes"> var
allowChar=Numbers+Seperater;
style="mso-spacerun: yes"> for
(i=0; i<ValLen; i++)
style="mso-tab-count: 1"> {
style="mso-spacerun: yes"> if((allowChar.indexOf(Val.charAt(i))
== -1))
style="mso-spacerun: yes">
{
style="mso-spacerun: yes">
object.value=Val.substring(0,i);
style="mso-spacerun: yes">
break;
style="mso-spacerun: yes">
}
style="mso-spacerun: yes"> if((style="color: #a31515">"01346789".indexOf(i)>-1)&&(Val.charAt(i)==Seperater))
style="mso-spacerun: yes"> {
style="mso-spacerun: yes">
object.value=Val.substring(0,i);
style="mso-spacerun: yes">
break;
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> }
style="mso-spacerun: yes"> if(object.value.length==2)
style="mso-spacerun: yes"> object.value=object.value+Seperater;
style="mso-spacerun: yes"> else
if(object.value.length==5)
style="mso-spacerun: yes"> object.value=object.value+Seperater;
style="mso-spacerun: yes"> }
No comments:
Post a Comment