Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

how to set a text box text to accept values in time format only.
Thanks
Posted
Updated 27-Jul-11 0:29am
v2

why not use ajax calender control?

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/MaskedEdit/MaskedEdit.aspx[^]

or use a regular expression validator:

http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]

Look here for regular expression for date:

http://regexlib.com/Search.aspx?k=Date%20validation[^]
 
Share this answer
 
Comments
thatraja 27-Jul-11 19:23pm    
This one enough for the OP, 5!
you use the custom validation for date validation or regularexpression validator for enter time in correct form you write java script for time validation which bind with coutom validation:
<pre lang="cs"><script type="text/javascript">
        function validTime(source,args)
        {
        var res=false;
        var orgVAlue=args.Value;
        var hour=orgVAlue.substring(0,2);
        var min=orgVAlue.substring(3,5);
        var sec=orgVAlue.substring(6,8);
        if(orgVAlue.length==0)
        res=false;
        else if(orgVAlue.length>8)
        res=false;
        else
        {
        if((hour>23)||(min>59)||(sec>59))
        res=false;
        else
        res=true;
        }
        args.IsValid=res;
        }
    </script>




now i use with text box like:-
<pre lang="xml"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="validTime"
            ControlToValidate="TextBox1" ErrorMessage="Not valid date" SetFocusOnError="True"
            ValidateEmptyText="True"></asp:CustomValidator>
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1"
            ErrorMessage="Enter date in dd:mm:ss" SetFocusOnError="True" ValidationExpression="dd:dd:dd"></asp:RegularExpressionValidator><br />
        <asp:Button ID="Button1" runat="server" Text="Button" />




Now you try this above code.
 
Share this answer
 
A validator will tell the user when they typed something wrong. To actually stop them typing it, you need to write javascript to parse input as they type. A better idea for time is to have drop downs for hours and for minutes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900