Hello,
I have a form that gets validated by using some javascript. Here is what i got
<script language=”JavaScript”>
function check_form()
{
var form = document.spform;
for(i=0;i<form.length;i++)
{
error = form.elements[i];
if(!error.value)
{
document.write(“errors inthe form “+ error.name);
error.focus;
return false;
}
}
return true;
}
</script>
what im stuck on is, when there is an error in the form, like the user is missing some text fields, it only displays the first field it finds, i want it to display all the fields that are null/empty. i think i need to have a for/foreach??? can anyone please help me with this?
Thanks
Dawn
Couple of things could be happening here. The most obvious is that you [i]return[/i] when you find your first error, if you want it to continue you'll have to move the return outside of the for loop.[/QUOTE]
Never tried doing this and haven't tested but you could try adding:
for(x=0;x<reqele.length;x++)
{
if(form.elements[i].name == reqele[x])
{
if((form.elements[i].value == "") || (form.elements[i].value == null))
[COLOR=Red]var message += form.elements[i].getAttribute('attributeName') + ',';[/COLOR]
//then you alert(message), however this assumes that you've given the elements names. You could also put a line break in place of the comma[/QUOTE]
By the way, you didn't need to create an array. The elements array is native to javascript. You reference it like so : document.forms[0].elements[i];
edit: geez, I must be asleep. I just realized you're iterating through the wrong array. Your for loop needs to be:
for(var i=0;i<form.elements.length;i++)[/QUOTE]
Sorry I should've said to put 'name' inside the quotes. For now, comment out all references to the array you created. Just use the for-loop for the elements array.[/QUOTE]
if((form.elements[i].value == "") || (form.elements[i].value == null))
{
var message = form.elements[i].getAttribute('attributeName') + ',';
document.write("The required fields are missing: ""+form.elements[i].name+"" was left blank");
//document.write("The required fields are missing: ""+form.elements[i].name+"" was left blank");
form[i].focus();
frm = false;
}
}
}
}
Sorry I should've said to put 'name' inside the quotes. For now, comment out all references to the array you created. Just use the for-loop for the elements array.[/QUOTE]
if((form.elements[i].value == "") || (form.elements[i].value == null))
{
var message = form.elements[i].getAttribute('name') + ',';
document.write("The required fields are missing: ""+message+"" was left blank");
//document.write("The required fields are missing: ""+form.elements[i].name+"" was left blank");
form[i].focus();
frm = false;
}
}
}
return frm;
}
No, none, and it's still there even if all the fields are filled. There are no other elements there. That's what I'm still working on. The form won't submit until that's solved.
I should also tell you that you're missing some required form attributes such as action and method.[/QUOTE]
<i>
</i>...
Last Name: <input type="text" name="lname" id="lname">
<i>
</i>document.getElementById("lname").value
getElementById requires an id="idname" attribute on an element and returns a single element (id's must be unique). So if you had:
<i>
</i>...
Last Name: <input type="text" name="lname" id="lname">
You could retrieve its value using:
[/QUOTE]
<i>
</i>document.getElementById("lname").value
0.1.9 — BETA 5.12