Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Textbox to dislplay Time (23:59) or empty string

Reply
Thread Tools

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

 
 
Joe Kovac
Guest
Posts: n/a
 
      05-02-2007
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
 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      05-02-2007
> 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


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      05-02-2007
"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

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      05-02-2007
"Hans Kesting" <> wrote in message
news: m...

> 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

 
Reply With Quote
 
Joe Kovac
Guest
Posts: n/a
 
      05-02-2007
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.
 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      05-02-2007
"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

 
Reply With Quote
 
Joe Kovac
Guest
Posts: n/a
 
      05-02-2007
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()?
 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      05-02-2007
"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?sourc...2+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/forum...ttextquot.html


--
http://www.markrae.net

 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      05-03-2007
> "Hans Kesting" <> wrote in message
> news: m...
>
>> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Empty gridview cells and checking for empty string Savvoulidis Iordanis ASP .Net 1 09-05-2008 06:15 AM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
Time.parse of empty string returns Time.now? Peter Krantz Ruby 2 08-20-2006 10:53 PM
FormView, empty TextBox gives error: Input string was not in a cor =?Utf-8?B?T3R0YXI=?= ASP .Net 6 05-24-2005 01:00 PM
FormView, empty TextBox gives error: Input string was not in a cor Neil Young ASP .Net 0 05-12-2005 09:29 PM



Advertisments