Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Time validation(hh:mm:ss) in .net??

Back to Forum: Tech Off
  • User profile image
    venkatzeus

    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

    Now i created a javascript function to check the validarion.In the web page i have 2 textbox, one for entering the name and the other for entering the time. I have a submit button. I have done a validation for the name textbox. so in the submit button, i have written as  OnClick="btnNew_Click" OnClientClick="return ValidateInput()". Is it possible to provide one more OnClientClick event?
    I want to validate the time also, so can i write 
    <asp:Button ID="btnNew" runat="server" CommandName="New"
       OnClick="btnNew_Click" OnClientClick="return ValidateInput()" OnClientClick ="return IsValidTime(document.timeform.time.value)"  Text="New" Width="48px" />
    If i cannot use like this, how can i provide two validations in the same click?

  • User profile image
    littleguru

    What I thought of is using Regex (the .NET class) to validate the content of the textbox (More on regex in general is found here). I have created a short sample - a website that contains a textbox and a button. The button has a the click event registered. Now when you click the button the event method is triggered and Regex is used to validate the input.

    The web site:

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
     
            <asp:TextBox runat="server" ID="timeTextBox"></asp:TextBox>
            <asp:Button runat="Server" ID="okButton" Text="OK" OnClick="okButton_Click" />
     
        </div>
        </form>
    </body>
    </html>


    The code beside file:

    public partial class _Default : System.Web.UI.Page
    {
        protected void okButton_Click(object sender, EventArgs e)
        {
            bool isOK = Regex.IsMatch(timeTextBox.Text, @"[0-2][0-9]\:[0-6][0-9]\:[0-5][0-9]");
            if (isOK)
            {
                // Do something.
            }
            else
            {
                // Do something else.
            }
        }
    }

  • User profile image
    littleguru

    venkatzeus wrote:
    Now i created a javascript function to check the validarion.In the web page i have 2 textbox, one for entering the name and the other for entering the time. I have a submit button. I have done a validation for the name textbox. so in the submit button, i have written as  OnClick="btnNew_Click" OnClientClick="return ValidateInput()". Is it possible to provide one more OnClientClick event?
    I want to validate the time also, so can i write 
    <asp:Button ID="btnNew" runat="server" CommandName="New"
       OnClick="btnNew_Click" OnClientClick="return ValidateInput()" OnClientClick ="return IsValidTime(document.timeform.time.value)"  Text="New" Width="48px" />
    If i cannot use like this, how can i provide two validations in the same click?


    You could write a javascript method that is calling both! You need to give that method all the arguments that are required by both methods and internally it is then calling both and returning true if both returned true - and false if one returned false!

  • User profile image
    Ion Todirel

    littleguru wrote:
    You could write a javascript method that is calling both! You need to give that method all the arguments that are required by both methods and internally it is then calling both and returning true if both returned true - and false if one returned false!
    or you could use ASP.NET validation controls Tongue Out its crazy to do such simple validation on server side Wink

  • User profile image
    W3bbo

    littleguru wrote:
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void okButton_Click(object sender, EventArgs e)
        {
            bool isOK = Regex.IsMatch(timeTextBox.Text, @"[0-2][0-9]\:[0-6][0-9]\:[0-5][0-9]");
            if (isOK)
            {
                // Do something.
            }
            else
            {
                // Do something else.
            }
        }
    }


    That wouldn't work, values like 29:67:00 are reported as valid.

    I'd suggest just doing DateTime.TryParse

  • User profile image
    stevo_

    Personally I'd avoid recreating the wheel with regex, and go with the DateTime parsing.

    Use TryParseExact

    DateTime.TryParseExact(input, "H:m:s", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, New DateTime)

    That would allow, 1:1:1, if you want to only allow, 01:01:01 then use "HH:mm:ss".

  • User profile image
    littleguru

    stevo_ wrote:
    Personally I'd avoid recreating the wheel with regex, and go with the DateTime parsing.

    Use TryParseExact

    DateTime.TryParseExact(input, "H:m:s", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, New DateTime)

    That would allow, 1:1:1, if you want to only allow, 01:01:01 then use "HH:mm:ss".



    Yes. That would work! Sometimes the solution is near, but we are unable to see it Tongue Out

    My Regex expression was really faulty. Parsing is way better!

  • User profile image
    odujosh

    As Hansleman says: Sometimes you have problem. You try to solve it with Regulaur Expressions and end up with two problems.
    Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.