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

Discussion in 'Javascript' started by Matt Herson, Sep 30, 2003.

  1. Matt Herson

    Matt Herson Guest

    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
    Matt Herson, Sep 30, 2003
    #1
    1. Advertising

  2. "Matt Herson" <> 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;
    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 30, 2003
    #2
    1. Advertising

  3. Matt Herson

    Matt Herson Guest

    Re: form validation that will display text at top of page if fields are blank - getting closer

    "Lasse Reichstein Nielsen" <> wrote in message
    news:...
    > "Matt Herson" <> 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;
    > 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 -
    > 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
    Matt Herson, Sep 30, 2003
    #3
  4. Matt Herson

    Matt Herson Guest

    Re: form validation that will display text at top of page if fields are blank - getting closer

    "Matt Herson" <> wrote in message
    news:blcf4l$hvl$...
    >
    > "Lasse Reichstein Nielsen" <> wrote in message
    > news:...
    > > "Matt Herson" <> 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;
    > > 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 -
    > > 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?
    Matt Herson, Sep 30, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sumith Mathur
    Replies:
    0
    Views:
    2,201
    Sumith Mathur
    Sep 23, 2004
  2. Replies:
    2
    Views:
    1,037
    Adrienne Boswell
    Apr 4, 2008
  3. Matthew Margolis

    Ruby CGI and blank form fields

    Matthew Margolis, Jan 30, 2005, in forum: Ruby
    Replies:
    2
    Views:
    125
    Martin DeMello
    Jan 30, 2005
  4. bnp
    Replies:
    4
    Views:
    318
  5. rob c
    Replies:
    4
    Views:
    316
    McKirahan
    Dec 30, 2005
Loading...

Share This Page