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

J

Joe Kovac

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
 
H

Hans Kesting

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
 
M

Mark Rae

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;
}
 
M

Mark Rae

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...?
 
J

Joe Kovac

Mark said:
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.
 
M

Mark Rae

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...
 
J

Joe Kovac

Mark said:
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()?
 
M

Mark Rae

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
How about onchange()?

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

Hans Kesting

Hans Kesting said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top