C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Javascript »

Validate a date Field using javascript


Posted Date: 05 Aug 2009    Resource Type: Code Snippets    Category: Javascript
Author: MohanakrishnanMember Level: Gold    
Rating: 1 out of 5Points: 7



Description :


Using javascript we can do lot of things, here a sample to validate a date in a textbox.

Passing the textbox name to the function, it will check the date is valid or not.

Here some Regular expression is used for validation.

Code:
-----

function valdate(id)
{
if(document.getElementById(id).value != "")
{
//alert(document.getElementById(id).value.length)
if(document.getElementById(id).value.length >= 12)
{
alert('please Enter date in dd/mmm/yyyy format');
document.getElementById(id).value = '';
document.getElementById(id).focus();
}
else
{
myregexp = /(0[1-9]|[12][0-9]|3[01])[- /.](Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[- /.](19|20)\d\d/

mymatch = myregexp.exec(document.getElementById(id).value)
//alert(mymatch);
if(mymatch==null)
{
alert('please Enter date in dd/mmm/yyyy format');
document.getElementById(id).focus();
document.getElementById(id).value = '';
}
else
{
var temp1 = document.getElementById(id).value;
//alert(temp1);
var temp = temp1.split('/');
//alert('test')
if(temp[1] == 'Feb')
{
var Lyear = temp[2]%4
Lyearreg = /(0[1-9]|[12][0-9])/
nLyearreg = /(0[1-9]|[12][0-8])/
if(Lyear == 0)
{
//alert('test')
Lmatch = Lyearreg.exec(temp[0])
if(Lmatch == null)
{
alert('please Check the Date and Month');
document.getElementById(id).value = '';
document.getElementById(id).focus();
}
}
else
{
nLmatch = nLyearreg.exec(temp[0]);
if(nLmatch == null)
{
alert('please Check the Date and Month');
document.getElementById(id).value = '';
document.getElementById(id).focus();
}
}
}
Dmonthreg = /(Jan|Feb|Mar|May|Jul|Aug|Oct|Dec)/
Dmonth = Dmonthreg.exec(temp[1]);
//alert(Dmonth);
if(Dmonth == null)
{
if(temp[0] == 31)
{
alert('please Check the Date and Month');
document.getElementById(id).value = '';
document.getElementById(id).focus();
}
}


}
}
}
}


Note:
-----
It validate only dd/MMM/yyyy format.



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Validation  .  Javascript  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How do you access the server side function using javascript in asp.net 3.5?
Previous Resource: Validate the character and Numbers
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use