Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

time input validation

Status
Not open for further replies.

bbvic

Technical User
Oct 21, 2004
51
US
hi,
I have time validation for one input textfield.
this time, I am trying to check for more than one textfields. But my code does not work...
Would you please help me??
Thank you in advance
=============

<html>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function IsValidTime() {
for (i = 0; i <= 3; i++)
{

var timeStrA = document.timeform.time +'i'.value;
var timeStr = timeStrA+'i';
var timePat = /^(\d{1,2}):(\d{2})?$/;

var matchArrayA= timeStr.match(timePat);
var matchArray = matchArrayA +'i';
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}

var hour = matchArray[1];
var minute = matchArray[2];

if (hour < 0 || hour > 24) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}

if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
}
}

</script>
</HEAD>


<BODY>

<center>
<form name=timeform onSubmit="return IsValidTime()">

time1 <input type=text name="time"><br>
time2 <input type=text name="time1"><br>
time3 <input type=text name="time2"><br>
time4 <input type=text name="time3"><br>

<input type="submit" value="Submit" >
</form >
</center>

</body>
</html>
 

Change this:

Code:
var timeStrA = document.timeform.time +'i'.value;

to this:

Code:
var timeStrA = document.forms['timeform'].elements['time + 'i'].value;

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
if that doesn't help, try this:

Code:
<html>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

function IsValidTime(f) {
    var theName = 'time';
    var els = f.elements;
    
    var timeStrA, timeStr, timePat, matchArrayA, matchArray, hour, minute;
    
    for ( var i = 0; i < els.length; i++ ) {
        if (els[i].name.indexOf(theName) > -1) {
            timeStrA = els[i].value;
            timePat = /^(\d){1,2}:(\d{2})?$/;
            
            matchArrayA = timeStrA.match( timePat );
            
            if (matchArrayA == null) {
                alert("Time is not in a valid format.");
                els[i].focus();
                return false;
            }
            
            hour = matchArrayA[1];
            minute = matchArrayA[2];
            
            if (hour < 0  || hour > 24) {
                alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
                els[i].focus();
                return false;
            }
            
            if (minute<0 || minute > 59) {
                alert ("Minute must be between 0 and 59.");
                els[i].focus();
                return false;
            }
        }
    }
}

</script>
</HEAD>


<BODY>

<center>
<form name=timeform onSubmit="return IsValidTime(this)">

time1  <input type=text name="time"><br>
time2  <input type=text name="time1"><br>
time3  <input type=text name="time2"><br>
time4  <input type=text name="time3"><br>

<input type="submit" value="Submit" >
</form >
</center>

</body>
</html>

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Thank you.
If I change input name="time" to input name ="time0"
and
var theName ='time' to var theName ='time0' to, Then
it does not work.
Would you please help me where I miss??
Thank you again
 
leave theName as time. it will check all fields with the string "time" somewhere in the name. so it will check fields with the following names:

time
time0
time1
time2

but it will not check:

submit
rdoSomething
tim
bob

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top