Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > form validation that will display text at top of page if fields are blank

Reply
Thread Tools

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

 
 
Matt Herson
Guest
Posts: n/a
 
      09-30-2003
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


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-30-2003
"Matt Herson" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Matt Herson
Guest
Posts: n/a
 
      09-30-2003

"Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Matt Herson" <(E-Mail Removed)> 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 - (E-Mail Removed)
> 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


 
Reply With Quote
 
Matt Herson
Guest
Posts: n/a
 
      09-30-2003

"Matt Herson" <(E-Mail Removed)> wrote in message
news:blcf4l$hvl$(E-Mail Removed)...
>
> "Lasse Reichstein Nielsen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > "Matt Herson" <(E-Mail Removed)> 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 - (E-Mail Removed)
> > 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
HTML Submit Blank Form Fields kagreen2002@yahoo.com HTML 2 04-04-2008 01:34 AM
<form>...</form> - how to supress blank space after </form> in IE? rob c Javascript 4 12-30-2005 06:10 PM
Struts validation for combine form fields validation Sumith Mathur Java 0 09-23-2004 06:26 PM
Form Validation Problem...Persisiting form fields on validation failure. bnp Javascript 4 05-12-2004 12:16 PM



Advertisments