C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Javascript function for validating Date fields


Posted Date: 18 Jan 2008    Resource Type: Code Snippets    Category: Validations

Posted By: Dhivya       Member Level: Silver
Rating:     Points: 10



There are validation controls in asp.net. By using JavaScript control validations can be done the code below says how a date is validated when it is given in a TextBox.
The TextBox Value is passed as,
var dob=document.getElementById("<%=txtDOB.ClientID%>").value;
validate_required(dateofbirth,"Date Of Birth") // calls the function

We can put the JavaScript function in a common .js file call that file where ever needed.



function validate_Date(CtrlValue,CtrlCaption)
{
// dtStr=dtStr
var separator= "/";
var minYear=1900;
var maxYear=2100;
var daysInMonth = DaysArray(12)
var pos1=CtrlValue.indexOf(separator)
var pos2=CtrlValue.indexOf(separator,pos1+1)
var strDay=CtrlValue.substring(0,pos1)
var strMonth=CtrlValue.substring(pos1+1,pos2)
var strYear=CtrlValue.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++)
{
if (strYear.charAt(0)=="0" && strYear.length>1) strYear=strYear.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYear)
if (pos1==-1 || pos2==-1)
{
alrtMsg+="The date format should be : mm/dd/yyyy"+"\n"
}
else if (strMonth.length<1 || month<1 || month>12)
{
alrtMsg+="Please enter a valid month"+"\n"
}
else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
{
alrtMsg+="Please enter a valid day"+"\n"
}
else if (strYear.length != 4 || year==0 || yearmaxYear)
{
alrtMsg+="Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"\n"
}
else if (CtrlValue.indexOf(separator,pos2+1)!=-1 || isInteger(stripCharsInBag(CtrlValue, separator))==false)
{
alrtMsg+="Please enter a valid date"+"\n"
}
}


function AlrtMsg(alrtMsg)
{

if(alrtMsg.length >= 0)
{

alert(alrtMsg);
//alrtMsg+="\n";
return true;
}

else if(alrtMsg.length == 0)
{
return false;
}
}







Responses


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

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: Data Validation
Previous Resource: Validating a textbox to enter only numerics and no special characters or characters
Return to Discussion Resource Index
Post New Resource
Category: Validations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

efax

Contact Us    Privacy Policy    Terms Of Use