View Full Version : Validate Time
LeahSmart
04-20-2004, 08:25 AM
Hi Reader!
I have a text field where a user enters a time in the format of hh:mm:ss, they can just enter hh:mm and it still works. Is there any way to validate this? So for example the user could not enter 99:99 which is not a valid time.
Any help would be great.
Thanks
Leah
CyanBlue
04-20-2004, 08:35 AM
Um... What if you check if the value is greater than 59 right before the focus on the textField goes away???
black
04-20-2004, 08:35 AM
myText.onChange = function()
{
var _timeArr = this.text.split(":");
if (Number(_timeArr[0]) >= 24 || Number(_timeArr[1]) > 60 || Number(_timeArr[2]) > 60)
{
trace("invalid input~");
}
}
LeahSmart
04-20-2004, 08:38 AM
Black, that looks good. The only thing is if they do not enter a : or just generally are really stupid users and put the : in the wrong place. How about having two boxes, one for the hour and one for the minute? The the code above would work really well.
CyanBlue
04-20-2004, 08:43 AM
Textfield can restrict the user input...
Check out TextField.restrict and TextField.maxChars from the Flash manual for more information...
black
04-20-2004, 08:46 AM
absolutely you can pick 3 boxes or just one, either will go well if we apply accurate code to it. and if you want to report an error if user forget to input an ":" or place it at wrong place just fix the if condition like this:
var _result = true;
if (_timeArr.length == 3 && !isNaN(_timeArr[0]) && !isNaN(_timeArr[1]) && !isNaN(_timeArr[2]))
{
if (Number(_timeArr[0]) >= 24 || Number(_timeArr[1]) > 60 || Number(_timeArr[2]) > 60)
{
_result = false;
}
}
else
{
_result = false;
}
if (!_result) trace("invalid input !");
black
04-20-2004, 08:48 AM
hey CranBlue why you always post so fast ? i lose man...;):D
CyanBlue
04-20-2004, 08:55 AM
That's because I don't give out the code like you do... :p Good job, man... :)
black
04-20-2004, 09:11 AM
;):D
LeahSmart
04-20-2004, 09:18 AM
I have added the the code below to the onchange but it always says it's invalid even when it is not.
on (change){
var _result = true;
if (_timeArr.length == 3 && !isNaN(_timeArr[0]) && !isNaN(_timeArr[1]) && !isNaN(_timeArr[2]))
{
if (Number(_timeArr[0]) >= 24 || Number(_timeArr[1]) > 60 || Number(_timeArr[2]) > 60)
{
_result = false
}
}
else
{
_result = false;
}
if (!_result) trace("invalid input !");
if (_result) trace("valid input !");
}
black
04-20-2004, 09:32 AM
i' sorry for missing something, try this:
if (_timeArr.length == 3 && !isNaN(Number(_timeArr[0])) && !isNaN(Number(_timeArr[1])) && !isNaN(Number(_timeArr[2])))
Regards
LeahSmart
04-20-2004, 09:38 AM
I entered 10:15 and it said it was invalid.
black
04-20-2004, 09:45 PM
it will because it can only accept 3 parts: hours, minutes and seconds(you can see the code: _timeArr.length == 3) and if you want just 2 you can fix the number 3 to 2 and remove code: !isNaN(Number(_timeArr[2])) and its relation.
Regards~
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.