473,554 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on For Loops Using ID Number

Hello,

I posted a question regarding data validation earlier and it worked
great! I have a new question. Is there anyway to cut through all the
if statements and just have a for loop that uses the id numbers
associated with each input. Here's the code I have so far:

<html>
<head>
<script type="text/javascript">

function main(form)
{
if (form.descripti on.value =='')
{
alert("you have not entered in a description");
return false;
}
if (form.awarddate .value =='')
{
alert("you have not entered in an award date2");
return false;
}
if(form.awardda te2.value =='')
{
alert("you have not entered in an award date2");
return false;

return true;
}

</script>
</head>
<body>
<form action="submit. html" method="post" name="input" onsubmit="retur n
main(this)">
<table>
<tr>
<th>Rick's Test Page<br><br></th>
</tr>
<tr>
<td>Description :<td><input type=text name="descripti on" id=1>
</tr><tr>
<td>Award Date:<td><input type=text name="awarddate " id=2>
</tr><tr>
<td>Award Date 2:<td><input type=text name="awarddate 2" id=3>
</tr>
</table>
<input type="submit" value="Submit" class="FormText ">
</form>
</body>
</html>

So In this case I'd want to make a for loop that loops from id=1 to
id=3. Is there anyway to do that? Thanks for the help!

Rick

Oct 24 '05 #1
5 1301
"Rick" <RS********@gma il.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hello,

I posted a question regarding data validation earlier and it worked
great! I have a new question. Is there anyway to cut through all the
if statements and just have a for loop that uses the id numbers
associated with each input. Here's the code I have so far:


[snip]

You're code is missing a "}" before "return true;".

You're missing closing </td> tags.

Don't use "main()" as it's a rserved word (per Evertjan).

Though you can use "document.getEl ementById()" it isn;t necessary.

The following loops through all form elements and checks all "text"
elements to see if they're blank.

Also, the fields name (name=" is displayed in the alert so it is suggested
that you assign a anme similar to the label they'll see on the page.

<html>
<head>
<script type="text/javascript">
function validate(form) {
var fail = "";
for (i=0; i<form.elements .length; i++) {
if (form.elements[i].type == "text") {
if (form.elements[i].value == "") {
fail += "\n\t" + form.elements[i].name + "";
}
}
}
if (fail != "") {
alert("The following fields are blank:\n" + fail);
return false;
}
return true;
}
</script>
</head>
<body>
<form action="submit. html" method="post"
name="input" onsubmit="retur n validate(this)" >
<table>
<tr>
<th colspan="2">Ric k's Test Page<br><br></th>
</tr>
<tr>
<td>Description :</td>
<td><input type=text name="Descripti on" id=1></td>
</tr>
<tr>
<td>Award Date:</td>
<td><input type=text name="Award_Dat e" id=2></td>
</tr>
<tr>
<td>Award Date 2:</td>
<td> <input type=text name="Award_Dat e_2" id=3></td>
</tr>
</table>
<input type="submit" value="Submit" class="FormText ">
</form>
</body>
</html>
Oct 24 '05 #2
Amazing!

Thank you so much!

Oct 24 '05 #3
McKirahan wrote:
"Rick" <RS********@gma il.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.com...
Hello,

I posted a question regarding data validation earlier and it worked
great! I have a new question. Is there anyway to cut through all the
if statements and just have a for loop that uses the id numbers
associated with each input. Here's the code I have so far:

[snip]

You're code is missing a "}" before "return true;".

You're missing closing </td> tags.

Don't use "main()" as it's a rserved word (per Evertjan).

Though you can use "document.getEl ementById()" it isn;t necessary.

The following loops through all form elements and checks all "text"
elements to see if they're blank.

Also, the fields name (name=" is displayed in the alert so it is suggested
that you assign a anme similar to the label they'll see on the page.


[...]
<td><input type=text name="Descripti on" id=1></td>


And ID's can't start with a number, an id:

"must begin with a letter ([A-Za-z]) and may be followed by any number
of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons
(":"), and periods (".")."

<URL:http://www.w3.org/TR/html4/types.html#type-name>

[...]
--
Rob
Oct 24 '05 #4
"RobG" <rg***@iinet.ne t.au> wrote in message
news:43******** **************@ per-qv1-newsreader-01.iinet.net.au ...

[snip]
<td><input type=text name="Descripti on" id=1></td>


And ID's can't start with a number, an id:

"must begin with a letter ([A-Za-z]) and may be followed by any number
of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons
(":"), and periods (".")."

<URL:http://www.w3.org/TR/html4/types.html#type-name>

[...]


Also, "id=" is usually set to the same value as "name=".
Oct 24 '05 #5
McKirahan wrote on 24 okt 2005 in comp.lang.javas cript:
Also, "id=" is usually set to the same value as "name=".


Not at all.

In a form, the name goes into the querystring/poststring,
and could be out of control of the programmer on a external site,
the id however could be seralized.

<input name='theName' id='f1'>
<input name='theStreet ' id='f2'>
<input name='theTown' id='f3'>
......

for large forms, the clientside code can be much simpler that way:

for (i=1;i<60;i++){
with (document.getEl ementById('f'+i )){
if (value != defaultValue)
style.backgroun dColor='cyan'
else
style.backgroun dColor=''
}
}

yes I know, IE only can do:

input { background-color:expressio n(value!=defaul tValue?'cyan':' ');}

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 25 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2637
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the same speed? Or, would the array process faster? I'm new to Python, so my question may expose my ignorance. I appreciate anyone's effort to help me...
3
1941
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm tending towards the use of generators. One good example is if I want to print a report, or to work over a list with complex processing for each...
5
5514
by: John Edwards | last post by:
Hello, I have sort of a newbie question. I'm trying to optimize a loop by breaking it into two passes. Example: for(i = 0; i < max; i++) {
15
6566
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
2
2095
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths. i don't really understand what is expected, or really what the question means. could anyone explain what the question's after please? any help much...
5
2106
by: Randall Skelton | last post by:
Greetings all, I am trying to do what should be a simple join but the tables are large and it is taking a long, long time. I have the feeling that I have stuffed up something in the syntax. Here is what I have: telemetry=> select (tq1.timestamp = tq2.timestamp) as timestamp, tq1.value as q1, tq2.value as q2 from cal_quat_1 tq1 inner...
4
1463
by: James | last post by:
How can I see/set the timeout for a postback? I have a button click event which loops through a DataSet and calls a stored procedure for every record in the dataset. This can be a very large number, and locally it's working fine. However, when I move to our development server, the exact same code pointing to the same database redirects to a...
10
1336
by: Dick Moores | last post by:
I'm still trying to understand classes. I've made some progress, I think, but I don't understand how to use this one. How do I call it, or any of its functions? It's from the Cookbook, at <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/523048>. Thanks, Dick Moores =====================================================
5
1735
by: Kris Kowal | last post by:
I had a thought that might be pepworthy. Might we be able to break outer loops using an iter-instance specific StopIteration type? This is the desired, if not desirable, syntax:: import string letters = iter(string.lowercase) for letter in letters: for number in range(10): print letter, number
0
7542
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7824
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6176
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5452
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5170
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
863
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.