By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,628 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,628 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Matt Herson" <eb***@mherson.com> writes:
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[i];
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>
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:r8**********@hotpop.com...
"Matt Herson" <eb***@mherson.com> writes:
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[i];
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>
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


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
Jul 20 '05 #3

P: n/a

"Matt Herson" <eb***@mherson.com> wrote in message
news:bl**********@bob.news.rcn.net...

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:r8**********@hotpop.com...
"Matt Herson" <eb***@mherson.com> writes:
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[i];
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>
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


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?
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.