|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Loop through form fields
I gave up on Javascript a while ago and now it's haunting me again. I have a form... a huge form. I want to be able to cycle through the form fields to see if they're all blank or not. I don't know if there is a way to reference form elements by index number and do something magical such as a For loop where I can say formname.getElement[i] and just go through them all.
Anyone able to decipher what I want and tell me what I have to do to do this? |
|
#2
|
|||
|
|||
|
try
formname.item(index).value |
|
#3
|
|||
|
|||
|
try
formname.item(index).value |
|
#4
|
|||
|
|||
|
I have used the if statement:
if (formname.elements[i].value=="") { alert("your message") } I hope this helps. |
|
#5
|
||||
|
||||
|
Thanks!
What I ended up was using a combination of this script and this submit button at the bottom of the form. Worked great! Code:
<script language="JavaScript" type="text/javascript">
function checkForEmpties(form) {
var counter;
var formElements = form.elements;
var proceed;
for (i=0; i<formElements.length; i++) {
return(formElements[i].value!="");
}
}
</script>
.
.
.
.
.
<input name="Submit" type="submit" value="Search for Matching Records" onClick="if (!checkForEmpties(this.form)){alert('Please provide search criteria.'); return false;}">
|
|
#6
|
||||
|
||||
|
ok, scratch that. it doens't work afterall. I need help now :-/
This returns the FIRST element. I need it to check to see if all are false etc. Help please? |
|
#7
|
||||
|
||||
|
Maybe this one works. It SEEMS to work for me. Take the code if you want it. I promise nothing.
Code:
<script language="JavaScript" type="text/javascript">
function checkForEmpties(form) {
var i;
var formElements = form.elements;
var proceed;
for (i=0; i<formElements.length; i++) {
if (formElements[i].type == "text") {
if (formElements[i].value != "") {
return true;
break;
}
}
else if (formElements[i].type == "checkbox") {
if (formElements[i].checked) {
return true;
break;
}
}
}
return false;
}
</script>
|
|
#8
|
|||
|
|||
|
Your code looks very promising, think i'll use it sometime, seems to make validation a whole lot easier...Thanks for the offer
. One thing though is that if i use this script how do i call up an alert box if any one of the fields are empty or unchecked.....also how do i trap values in the fields that have already been filled instead of having the user fill out the whole form again instead of just the empty fields |
|
#9
|
||||
|
||||
|
what you need for some real validation
If you're interested in straight form validation I would use the attached files. Together they're GREAT.
The code that I posted will search through a form and if any of hte boxes are empty it will simply not submit the form. It worked for what I had to do but what I'm posting might be better. What it does is go through a form and if a form is blank but you require it to be filled, the form will alert the user that the form is empty and then give focus to the part of the form that is not correct. It's a little difficult to learn at first but you should get it. I'm an absolute pro at it after using it about 100 times literally. Took about 10 to know what I was doing. I broke it so many times. Use mozilla's built in javascript debugging for help on this one. Your form will be GREAT. |
|
#10
|
|||
|
|||
|
Thanks for the script I will use it and get back to u. Its a script that I have been looking for it for a long time now....
|
|
#11
|
||||
|
||||
|
well it's the best one I've found and those folks at Netscape know their stuff. It's VERY cross-browser friendly. I have not found a browser that does not work with it yet. For cross-browser compatibility stay away from Microsoft's scripts.
|
|
#12
|
|||
|
|||
|
Hey, which mozilla build has debugging for javascript?
|
|
#13
|
||||
|
||||
|
all. all you have to do on a page is type "javascript:" (without the quotes) into the address bar and the javascript debugger comes up. Same goes for Netscape and Mozilla Firefox.
__________________
If you found a post of mine helpful, please click on the on my post to add to my reputation.
|
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Loop through form fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|