[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Time input validation

Posted on 2005-04-18
7
Medium Priority
?
711 Views
Last Modified: 2008-02-01
hi!
I am trying to make input validation.
if I have one input text, the validation works. But if I have more than one input text, it does not work..

Would you please help me??

<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 matchArray +'i'  = timeStr.match(timePat);
var matchArray = matchArray +'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 >

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" onClick="return IsValidTime();">
</form>
</center>

</body>
</html>
0
Comment
Question by:bbvic
  • 5
7 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 13806346
Try:

var timeStrA = eval("document.timeform.time" +i + ".value");

-r-
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13806354
try
var timeStrA = document.timeform.elements[time +'i'].value;
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13806358
sorry shud be

var timeStrA = document.timeform.elements['time' +i].value;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 32

Expert Comment

by:Batalf
ID: 13806359
or simply

var timeStrA = document.timeform.elements['time' +i].value;
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13806363
<no-points>
sorry Roonan, did not see ur post
</no-points>
0
 
LVL 25

Expert Comment

by:archrajan
ID: 13806410
few other things need to be corrected in ur script


<html>

<HEAD>

<SCRIPT LANGUAGE="JavaScript">

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

var timeStr = document.timeform.elements['time' +i].value;

var timePat = /^(\d{1,2}):(\d{2})?$/;

var matchArray   = timeStr.match(timePat);

if (matchArray == null) {
alert("Time is not in a valid format.");
document.timeform.elements['time' +i].select();
document.timeform.elements['time' +i].focus();
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 >

time1  <input type=text name="time0"><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" onClick="return IsValidTime();">
</form>
</center>

</body>
</html>
0
 
LVL 25

Accepted Solution

by:
archrajan earned 2000 total points
ID: 13806466
Also include these 2 statements

document.timeform.elements['time' +i].select();
document.timeform.elements['time' +i].focus();

after the 2 alert statements for checking the minute and hour respectively.. adding those 2 lines u will know which textbox has the error!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question