Textbox to dislplay Time (23:59) or empty string

Discussion in 'ASP .Net' started by Joe Kovac, May 2, 2007.

  1. Joe Kovac

    Joe Kovac Guest

    Hi!

    I want the user to edit a textbox which allows following values only:
    - Time (Format: 23:59, HH:MM)
    or
    - NULL (empty string)
    What can I do, so that this works kinda automatically, meaning on the
    client side. It would be also nice, that not only errors would be
    detected, but also that "wanted error" would be corrected if possible,
    e.g. exchange "30" by "00:30".

    Which control and properties do I need? As a reminder, I do not use a
    bound field but a TextBox.

    Thanks

    Joe
    Joe Kovac, May 2, 2007
    #1
    1. Advertising

  2. Joe Kovac

    Hans Kesting Guest

    > Hi!
    >
    > I want the user to edit a textbox which allows following values only:
    > - Time (Format: 23:59, HH:MM)
    > or
    > - NULL (empty string)
    > What can I do, so that this works kinda automatically, meaning on the
    > client side. It would be also nice, that not only errors would be
    > detected, but also that "wanted error" would be corrected if possible,
    > e.g. exchange "30" by "00:30".
    > Which control and properties do I need? As a reminder, I do not use a
    > bound field but a TextBox.
    >
    > Thanks
    >
    > Joe
    >


    You can add a RegularExpressionValidator.
    Have the ControlToValidate property point to your textbox
    and set the ValidationExpression to [0-2]?[0-9]:[0-5][0-9]
    Set the ErrorMessage to the message you want to display if an error
    is made.

    This will validate that it looks like a correct time, or that nothing
    is entered.

    Note: the code that want this value (tries to store it?) should check
    Page.Isvalid before using it.

    Hans Kesting
    Hans Kesting, May 2, 2007
    #2
    1. Advertising

  3. Joe Kovac

    Mark Rae Guest

    "Joe Kovac" <> wrote in message
    news:728f0$46387352$3e63c322$...

    > Hi!
    >
    > I want the user to edit a textbox which allows following values only:
    > - Time (Format: 23:59, HH:MM)
    > or
    > - NULL (empty string)
    > What can I do, so that this works kinda automatically, meaning on the
    > client side. It would be also nice, that not only errors would be
    > detected, but also that "wanted error" would be corrected if possible,
    > e.g. exchange "30" by "00:30".
    >
    > Which control and properties do I need? As a reminder, I do not use a
    > bound field but a TextBox.


    I have a JavaScript function for this:

    function isTime(pstrTime)
    {
    var strValidChars = "0123456789:";
    var strChar;

    if (pstrTime.length != 5) return false;

    for (intChar = 0; intChar < pstrTime.length; intChar++)
    {
    strChar = pstrTime.charAt(intChar);
    if (strValidChars.indexOf(strChar) == -1)
    {
    return false;
    }
    }

    if (parseInt(pstrTime.substring(0, 2)) > 23) return false;
    if (pstrTime.substring(2, 3) != ":") return false;
    if (parseInt(pstrTime.substring(3)) > 59) return false;

    return true;
    }


    --
    http://www.markrae.net
    Mark Rae, May 2, 2007
    #3
  4. Joe Kovac

    Mark Rae Guest

    "Hans Kesting" <> wrote in message
    news:...

    > You can add a RegularExpressionValidator.
    > Have the ControlToValidate property point to your textbox
    > and set the ValidationExpression to [0-2]?[0-9]:[0-5][0-9]
    > Set the ErrorMessage to the message you want to display if an error
    > is made.
    >
    > This will validate that it looks like a correct time, or that nothing
    > is entered.


    Unless I'm very much mistaken, this would consider 29:59 as valid, wouldn't
    it...?


    --
    http://www.markrae.net
    Mark Rae, May 2, 2007
    #4
  5. Joe Kovac

    Joe Kovac Guest

    Mark Rae wrote:
    > "Joe Kovac" <> wrote in message
    > news:728f0$46387352$3e63c322$...
    >
    >> Hi!
    >>
    >> I want the user to edit a textbox which allows following values only:
    >> - Time (Format: 23:59, HH:MM)
    >> or
    >> - NULL (empty string)
    >> What can I do, so that this works kinda automatically, meaning on the
    >> client side. It would be also nice, that not only errors would be
    >> detected, but also that "wanted error" would be corrected if possible,
    >> e.g. exchange "30" by "00:30".
    >>
    >> Which control and properties do I need? As a reminder, I do not use a
    >> bound field but a TextBox.

    >
    > I have a JavaScript function for this:
    >
    > function isTime(pstrTime)
    > {
    > var strValidChars = "0123456789:";
    > var strChar;
    >
    > if (pstrTime.length != 5) return false;
    >
    > for (intChar = 0; intChar < pstrTime.length; intChar++)
    > {
    > strChar = pstrTime.charAt(intChar);
    > if (strValidChars.indexOf(strChar) == -1)
    > {
    > return false;
    > }
    > }
    >
    > if (parseInt(pstrTime.substring(0, 2)) > 23) return false;
    > if (pstrTime.substring(2, 3) != ":") return false;
    > if (parseInt(pstrTime.substring(3)) > 59) return false;
    >
    > return true;
    > }


    I will give this function a try and moreover I will try to include the
    auto complete that makes 00:30 out of 30.

    Thanks to all of you so far.
    Joe Kovac, May 2, 2007
    #5
  6. Joe Kovac

    Mark Rae Guest

    "Joe Kovac" <> wrote in message
    news:612ba$46387d08$3e63c322$...

    > I will give this function a try and moreover I will try to include the
    > auto complete that makes 00:30 out of 30.


    I would strongly advise against that sort of 'masked edit' functionality in
    ASP.NET, especially if you're intending to wire it up to one of the
    keystroke events of the TextBox...

    I always have a single routine which validates the entire page rather than
    individual control validation.

    However, if you absolutely must do autocomplete, consider doing it in the
    onblur() event...


    --
    http://www.markrae.net
    Mark Rae, May 2, 2007
    #6
  7. Joe Kovac

    Joe Kovac Guest

    Mark Rae wrote:
    > "Joe Kovac" <> wrote in message
    > news:612ba$46387d08$3e63c322$...
    >
    >> I will give this function a try and moreover I will try to include the
    >> auto complete that makes 00:30 out of 30.

    >
    > I would strongly advise against that sort of 'masked edit' functionality
    > in ASP.NET, especially if you're intending to wire it up to one of the
    > keystroke events of the TextBox...


    What exactly speaks against a (hopefully) comfortable auto completion?
    (I currently think about performance, browser incompatibility, etc.)

    > I always have a single routine which validates the entire page rather
    > than individual control validation.
    >
    > However, if you absolutely must do autocomplete, consider doing it in
    > the onblur() event...


    How about onchange()?
    Joe Kovac, May 2, 2007
    #7
  8. Joe Kovac

    Mark Rae Guest

    "Joe Kovac" <> wrote in message
    news:5487e$46388bd6$3e63c322$...

    > What exactly speaks against a (hopefully) comfortable auto completion? (I
    > currently think about performance, browser incompatibility, etc.)


    Both of those... If you absolutely must roll your own autocomplete, you must
    make absolutely sure that you switch the in-built autocomplete off for IE /
    Opera 9:
    http://www.google.co.uk/search?sour...BGB220GB220&q=<input type="text" autocomplete

    >> I always have a single routine which validates the entire page rather
    >> than individual control validation.
    >>
    >> However, if you absolutely must do autocomplete, consider doing it in the
    >> onblur() event...

    >
    > How about onchange()?


    onchange can be really irritating:
    http://www.velocityreviews.com/foru...tching-value-of-ltinput-typequottextquot.html


    --
    http://www.markrae.net
    Mark Rae, May 2, 2007
    #8
  9. Joe Kovac

    Hans Kesting Guest

    > "Hans Kesting" <> wrote in message
    > news:...
    >
    >> You can add a RegularExpressionValidator.
    >> Have the ControlToValidate property point to your textbox
    >> and set the ValidationExpression to [0-2]?[0-9]:[0-5][0-9]
    >> Set the ErrorMessage to the message you want to display if an error
    >> is made.
    >> This will validate that it looks like a correct time, or that nothing
    >> is entered.
    >>

    > Unless I'm very much mistaken, this would consider 29:59 as valid,
    > wouldn't it...?
    >


    You are right.
    I *knew* I had forgotten to check for those special situations :-(

    (([01]?[0-9])|2[0-3]):[0-5][0-9]


    Hans Kesting
    Hans Kesting, May 3, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?T3R0YXI=?=
    Replies:
    6
    Views:
    8,078
    =?Utf-8?B?TW9uZ283NDc=?=
    May 24, 2005
  2. flamesrock
    Replies:
    8
    Views:
    451
    Hendrik van Rooyen
    Nov 24, 2006
  3. Tzury Bar Yochay
    Replies:
    1
    Views:
    404
    Gabriel Genellina
    Mar 24, 2008
  4. Savvoulidis Iordanis

    Empty gridview cells and checking for empty string

    Savvoulidis Iordanis, Sep 5, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    520
    Savvoulidis Iordanis
    Sep 5, 2008
  5. Peter Krantz
    Replies:
    2
    Views:
    193
Loading...

Share This Page