form validation that will display text at top of page if fields are blank

M

Matt Herson

I am looking for a validation script that will only look at the fields in
the cgi form, determine if the fields are filled out, then if one or more
are blank will write a message at the top of the page stating that some
fields have not been filled out. I don't want a pop-up, I just want to send
them back. I had seen something that did this using <div /> tags but cant
seem to find one I can modify to work for this application.

I was using Jennifer Madden's steroid basic validator, but that wont work
for me as it requires all form names to use an asterisk if required as its
field name. This is not supported by the email result builder I am using.

Thanks!
-Matt
 
L

Lasse Reichstein Nielsen

Matt Herson said:
I am looking for a validation script that will only look at the fields in
the cgi form, determine if the fields are filled out, then if one or more
are blank will write a message at the top of the page stating that some
fields have not been filled out. I don't want a pop-up, I just want to send
them back. I had seen something that did this using <div /> tags but cant
seem to find one I can modify to work for this application.

Try something like this:
---
<script type="text/javascript">
function validate(form) {
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements;
if (elem.tagName == "INPUT" &&
(elem.type == "text" || elem.type == "password") ||
elem.tagName == "TEXTAREA") {
if (elem.value == "") {return false;}
}
}
return true;
}
function validateAndDisplay(form,div) {
div = document.getElementById(div);
if (!validate(form)) {
div.innerHTML = "Some fields still needs to be filled.";
return false;
} else {
div.innerHTML = "";
return true;
}
}
</script>

<div id="output"></div>
<form action="..." onsubmit="validateAndDisplay(this,'output')">
<input type="text" name="f1">
<input type="password" name="f2">
<textarea name="f3"></textarea>
<input type="submit" value="submit">
</form>
 
M

Matt Herson

Lasse Reichstein Nielsen said:
Matt Herson said:
I am looking for a validation script that will only look at the fields in
the cgi form, determine if the fields are filled out, then if one or more
are blank will write a message at the top of the page stating that some
fields have not been filled out. I don't want a pop-up, I just want to send
them back. I had seen something that did this using <div /> tags but cant
seem to find one I can modify to work for this application.

Try something like this:
---
<script type="text/javascript">
function validate(form) {
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements;
if (elem.tagName == "INPUT" &&
(elem.type == "text" || elem.type == "password") ||
elem.tagName == "TEXTAREA") {
if (elem.value == "") {return false;}
}
}
return true;
}
function validateAndDisplay(form,div) {
div = document.getElementById(div);
if (!validate(form)) {
div.innerHTML = "Some fields still needs to be filled.";
return false;
} else {
div.innerHTML = "";
return true;
}
}
</script>

<div id="output"></div>
<form action="..." onsubmit="validateAndDisplay(this,'output')">
<input type="text" name="f1">
<input type="password" name="f2">
<textarea name="f3"></textarea>
<input type="submit" value="submit">
</form>


Thanks! This script is working fine. Only thing now is I want to return
the entire form contents - not just the missing message. Is this done by
changing placement of the <div> tags or does the script need to be modified?
Thanks!
-Matt
 
M

Matt Herson

Matt Herson said:
to
send
them back. I had seen something that did this using <div /> tags but cant
seem to find one I can modify to work for this application.

Try something like this:
---
<script type="text/javascript">
function validate(form) {
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements;
if (elem.tagName == "INPUT" &&
(elem.type == "text" || elem.type == "password") ||
elem.tagName == "TEXTAREA") {
if (elem.value == "") {return false;}
}
}
return true;
}
function validateAndDisplay(form,div) {
div = document.getElementById(div);
if (!validate(form)) {
div.innerHTML = "Some fields still needs to be filled.";
return false;
} else {
div.innerHTML = "";
return true;
}
}
</script>

<div id="output"></div>
<form action="..." onsubmit="validateAndDisplay(this,'output')">
<input type="text" name="f1">
<input type="password" name="f2">
<textarea name="f3"></textarea>
<input type="submit" value="submit">
</form>


Thanks! This script is working fine. Only thing now is I want to return
the entire form contents - not just the missing message. Is this done by
changing placement of the <div> tags or does the script need to be modified?
Thanks!
-Matt

After further review, it looks like the message comes back for all
conditions. Even when the field is filled in. Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top