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 !






Time Validation (HH:MM AM/PM)


Posted Date: 31 May 2008    Resource Type: Code Snippets    Category: Validations

Posted By: Kapil Dhawan       Member Level: Gold
Rating:     Points: 10




Here is the code to validate time in format HH:MM AM/PM

Create a input type=Text with attribute runat and id
Create a input type=button with attributes runat,id and onClick

now on onClick of button we will write
"return IsValidTime(txtTime.value);"
where txtTime is the id of textbox


now in the Head Tag of the page we will write this function.
note this code will come between script tags.


function IsValidTime(timeStr)
{
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null)
{
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="")
{
second = null;
}
if (ampm=="")
{
ampm = null
}
if (hour < 0 || hour > 12)
{
alert("Hour must be between 1 and 12");
return false;
}
if(hour <= 12 && ampm == null)
{
alert("You must specify AM or PM.");
return false;

}
if (hour > 12 && ampm != null)
{
alert("Time is not in a valid format.");
return false;
}
if (minute<0 || minute > 59)
{
alert ("Minute must be between 0 and 59.");
return false;
}
if (second != null && (second < 0 || second > 59))
{
alert ("Second must be between 0 and 59.");
return false;
}
return false;
}






Responses

Author: thebarrett27    20 Jun 2008Member Level: Bronze   Points : 0
nice one
thanks


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: How to restrict length size in text box,memo box
Previous Resource: force control not to validate
Return to Discussion Resource Index
Post New Resource
Category: Validations


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use