Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]Time format hh:mm:ss[/b] 1

Status
Not open for further replies.

omlac

Programmer
Apr 7, 2008
13
ZA
hi, I have created a web application using ASP.NET. In that web page, i have a textbox, in which the user has to enter the time(hh:mm:ss) in this format. If the user doesnot enter the time in this format,ie(hh:m:ss), i have to provide an error message saying, time format is not proper. How to achieve this?
please help
 
that would be my choice, or for more sophistication you could use an ajax calander control with an ajax extender.

To go where no programmer has gone before.
 
thanks guys, i will try the thread but i dont like the calander coz my hh is limited to 23, i want to go up to 100 for hh.
 
As ca8msm states, there are many ways to do this.

The implementation you choose really depends on your particular project requirements though the following is an alternative to the above examples:

1. Create an .htc file (see sample code below)
2. Attach a css class to the text box
3. Have the css class point it's bahaviour to the htc file.

.htc file code

Code:
<PUBLIC:COMPONENT URN="hide">
	<PUBLIC:ATTACH EVENT="onfocus" ONEVENT="OnFocus()" />
	<PUBLIC:ATTACH EVENT="onblur" ONEVENT="OnBlur()" />
	<PUBLIC:ATTACH EVENT="onkeyup" ONEVENT="OnKeyUp()" />
	<PUBLIC:ATTACH EVENT="ondblclick" ONEVENT="OnDblClick()" />
	<PUBLIC:ATTACH EVENT="onkeydown" ONEVENT="OnKeyDown()" />
	<PUBLIC:ATTACH EVENT="onkeypress" ONEVENT="OnKeyPress()" />
	<PUBLIC:ATTACH EVENT="onclick" ONEVENT="OnClick()" />
	
	<script LANGUAGE="jscript">
	var ValidationFired;
	ValidationFired = false;
	var splitarr="/";

	function OnKeyDown()
	{ 
			if (event.keyCode==13) event.keyCode=9; 
	}
	
	function OnClick()
	{
		this.select();
	
	}

	function OnFocus()
	{
		//this.style.background='#99ccff'; 
	 }

	function OnBlur() 
	{
		if (this.value.length>6)
		{
			tmpArr = this.value.split(splitarr);
			
			if (tmpArr.length==3)
			{
				if(tmpArr[2].length==3)
				{
					dispAlert("Please Enter Valid Year");
					return;
				}
		
				if(tmpArr[2].length==2)
				//Transform a 2 digit yeat into a 4 digit year.
				var year = parseInt(tmpArr[2]);
				if(year < 50)
				{
				    this.value = this.value.substring(0,this.value.length-2) + "20"+tmpArr[2];
				}
				else
				{   
				    if(year < 100)
				    {
				        this.value = this.value.substring(0,this.value.length-2) + "19"+tmpArr[2];
				    }
				}
			}	
			
	    // ValidationFired = true;
	    }
	
	    if(!this.value.length) 
	    {
		    //this.style.background = '#FFEBE6';
		    return;
	    }
	
	    if(!ValidationFired)
	    {
	    //alert (this.value.length);
		    if(this.value.length < 10 || this.value.length >10 )
		    {
			    var retval=checkvaliddate(this);
			    if (retval==false)
			    {
				    dispAlert("Please Enter Valid Date");
				    this.focus();
				    return;
			    }	
		    }
		    else if (this.value.length == 10)
		    {
			    dtValue = this.value.split(splitarr);
			    lDate =  parseInt(dtValue[0],10);
			    lMonth = parseInt(dtValue[1],10);
			    lYear =  parseInt(dtValue[2],10);
			    if (!isDateJ(lMonth,lDate,lYear))
			    {
				    dispAlert("Please Enter Valid Date");
				    this.focus();
				    return;
			    }
		    }
	    }
        //	this.style.background = '#FFEBE6';
	}

	function OnDblClick()
	{
		/*
		marray=new Array();
		marray[0]=this;
		marray[1]="";
		showModalDialog("fncal.htm",marray,"border=thin;center=yes;dialogTop= 120px; dialogleft= 130px; dialogWidth=180px; dialogHeight=248px;help=0;status=no;maximize=no;minimize=no;status=no;scroll=no;");
		*/	
	}

	function OnKeyPress()
	{
	    window.event.keyCode=CheckKey(window.event.keyCode);	 
	    if(this.value.length==10){this.value = '';}	 
	}

	function OnKeyUp()
	{
	 Validatetime();
	}

	function CheckKey(keycode) 
	{
		 return ((keycode > 47 && keycode < 58) ? keycode : 0);
	}

	function Validatetime()
	{
		if(window.event.keyCode == 8 || window.event.keyCode == 36 || window.event.keyCode == 37 || window.event.keyCode == 46)	
		{
			this.value = "";
			return;
		}
	
		if(this.value.length==2)
		{
			if(this.value==0 || this.value>31)
			{
				dispAlert("Please Enter Valid Date");
				return;
			}
			else this.value += splitarr;
		}
		
		if(this.value.length==5)
		{
			var arr=this.value.split(splitarr);
			if(arr[1]==0 || arr[1]>12)
			{
				dispAlert("Please Enter Valid Month");
				return;
			}
			else this.value += splitarr;
		}
		 
		 
		ValidationFired=false;
	}

	function dispAlert(lMsg) {
	ValidationFired=true;
	alert(lMsg);
	this.value = "";
	}

	function isLeapYear(lYear) {
	if(((lYear%4 == 0) && (lYear%100 !=0)) || (lYear%400 ==0))
		return true;
	else
		return false;
	}

	function isDateJ(lMonth,lDate,lYear) {
		
	switch(lMonth) {
		case 4:
		case 6:
		case 9:
		case 11:
			if(lDate > 30) 	return false;
		break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if(lDate > 31) return false;
		break;	
		case 2:
			if(isLeapYear(lYear)) {
				if(lDate>29) return false;
			}
			else
				if(lDate>28) return false;
		break;		
	}
	return true;
	}
	
	
	
	
	
	
	
</script>

<script LANGUAGE="vbscript">
	function checkvaliddate(obj)
		checkvaliddate=isdate(obj.value)
	end function
</script>

</PUBLIC:COMPONENT>

We use this appraoch on a project that is used in multiple countries, each using a seperate .css file which points to a specific htc file and tehrefore date format.

The advantage of this approach is that it is easy to implement/maintain, and offers immediate feedback to the end user.

Of course, others may have other approaches that are more suitable to your project.

HTH

Smeat
 
smeat thanks very much, it worked perfectly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top