Wednesday, August 28, 2013

Time in TextBox (From - To)

Time in TextBox (From - To)
This I made for one of my clients. Hope its useful for you

First Add the Following Javascript int he head of your ASPX


function doTxt(txt)
    {  
      t=document.getElementById(txt);
     
      if(t.value.length !=0)
      {
        if(t.value.indexOf(':') == -1)
        {
          t.value=t.value+':00';
        }
        var firstOne = t.value.substring(0,1);
         
        if(document.getElementById(txt).value.toUpperCase().indexOf('AM') == -1 && document.getElementById(txt).value.toUpperCase().indexOf('PM') == -1 )
        {
        
          var firstTwo = t.value.substring(0,2);
         
          if(firstTwo.indexOf(':') == -1)
          {        
            if(parseInt(firstTwo) <=12)
            {           
              t.value=t.value+' PM';
            }   
             

          }
          else
          {      
            if(parseInt(firstTwo) <=12)
            {           
              t.value=t.value+' PM';
            }             
          }
        }
      }
    }
   
   
    function doTxtFrom(txt)
    {  
      t=document.getElementById(txt);
     
      if(t.value.length !=0)
      {
        if(t.value.indexOf(':') == -1)
        {
          t.value=t.value+':00';
        }
        var firstOne = t.value.substring(0,1);
         
        if(document.getElementById(txt).value.toUpperCase().indexOf('AM') == -1 && document.getElementById(txt).value.toUpperCase().indexOf('PM') == -1 )
        {
        
          var firstTwo = t.value.substring(0,2);
         
          if(firstTwo.indexOf(':') == -1)
          {         
            if(parseInt(firstOne) ==6 || parseInt(firstOne) ==7 ||parseInt(firstOne) ==8 || parseInt(firstOne) ==9 || parseInt(firstTwo) ==10 || parseInt(firstTwo) ==11)
            {
              t.value=t.value+' AM';
            }
            else if(parseInt(firstTwo) <=12)
            {           
              t.value=t.value+' PM';
            }            
          }
          else
          {
        
            if(parseInt(firstOne) ==6 || parseInt(firstOne) ==7 || parseInt(firstOne) ==8 || parseInt(firstOne) ==9 || parseInt(firstTwo) ==10 || parseInt(firstTwo) ==11)
            {
              t.value=t.value+' AM';
            }
            else if(parseInt(firstTwo) <=12)
            {           
              t.value=t.value+' PM';
            }           
          }
        }
      }
    }


2. Add the following Code in your CS

txtTimeFrom.Attributes.Add("onblur", "doTxtFrom('" + txtTimeFrom.ClientID + "');");


txtTimeTo.Attributes.Add("onblur", "doTxt('" + txtTimeTo.ClientID + "');");

No comments: