469,635 Members | 1,876 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,635 developers. It's quick & easy.

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

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
May 2 '07 #1
8 4540
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
May 2 '07 #2
"Joe Kovac" <Jo****@nospam.comwrote in message
news:72***************************@news.inode.at.. .
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

May 2 '07 #3
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0*************************@news.microsoft.co 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

May 2 '07 #4
Mark Rae wrote:
"Joe Kovac" <Jo****@nospam.comwrote in message
news:72***************************@news.inode.at.. .
>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.
May 2 '07 #5
"Joe Kovac" <Jo****@nospam.comwrote in message
news:61*************************@news.inode.at...
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

May 2 '07 #6
Mark Rae wrote:
"Joe Kovac" <Jo****@nospam.comwrote in message
news:61*************************@news.inode.at...
>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()?
May 2 '07 #7
"Joe Kovac" <Jo****@nospam.comwrote in message
news:54**************************@news.inode.at...
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

May 2 '07 #8
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:c0*************************@news.microsoft.co 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
May 3 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tetet B via .NET 247 | last post: by
11 posts views Thread by Dan Bass | last post: by
21 posts views Thread by M D | last post: by
26 posts views Thread by Neville Lang | last post: by
6 posts views Thread by mesut | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.