Data Validation Help When Submitting a Form

Discussion in 'Javascript' started by Rick, Oct 24, 2005.

  1. Rick

    Rick Guest

    Hello,

    I'm having trouble with submitting my form when checking to see if data
    is present in the user-inputted fields. What I want to happen is for
    the user to input various pieces of data, submit the form, and then
    have a javascript function that checks to see if data is entered, and
    if not, have an alert window tell the user which field to enter data
    into and then revert back to that same page. If all data is entered,
    it would submit the form and send the user to another page. However,
    I'm having trouble with reverting back to the orginal page for the user
    to update fields to. Here is my code for all other working parts:

    <html>
    <head>


    <script type="text/javascript">

    function main(form)
    {
    if(form.description.value =='')
    {
    alert("you have not entered in a description");
    }
    if(form.awarddate.value =='')
    {
    alert("you have not entered in an award date");
    }
    }
    </SCRIPT>
    </head>
    <body>
    <form name="input" action="submit.html" method="post">
    <table>


    Rick's Test Page</th><br><br>


    <tr>
    <td>Description:<td><input type=text name="description" id=1>
    </tr><tr>
    <td>Award Date:<td><input type=text name="awarddate" id=2>
    </tr>
    </table>
    <input type="submit" class="FormText" value="submit"
    onClick="main(input)">
    </form>
    </body>
    </html>

    Any ideas?...Thanks!

    Rick
     
    Rick, Oct 24, 2005
    #1
    1. Advertisements

  2. Rick

    McKirahan Guest

    [snip]

    Will this help? Watch for word-wrap.

    <html>
    <head>
    <script type="text/javascript">
    function main(form) {
    if (form.description.value =='') {
    alert("'Description' is missing.");
    return false;
    } else if (form.awarddate.value =='') {
    alert("'Award Date' is missing.");
    return false;
    }
    return true;
    }
    </script>
    </head>
    <body>
    <form action="submit.html" method="post"
    name="input" onsubmit="return main(this)">
    <table>
    <tr>
    <th colspan="2">Rick's Test Page<br><br></th>
    </tr>
    <tr>
    <td>Description: <input type="text" name="description"></td>
    </tr>
    <tr>
    <td>Award Date: <input type="text" name="awarddate"></td>
    </tr>
    </table>
    <br><br>
    <input type="submit" value="Submit" class="FormText">
    </form>
    </body>
    </html>
     
    McKirahan, Oct 24, 2005
    #2
    1. Advertisements

  3. Rick

    Evertjan. Guest

    Rick wrote on 24 okt 2005 in comp.lang.javascript:
    do not use a reserved name as a variable
    give a return false if the form should not be submitted yet
    put asn onsubmit here
    if the fields are submitted by giving enter the main() will not be
    invoked
    Try this lean code,
    that will only submit if both input fields are filled:


    <html>
    <head>

    <script type="text/javascript">

    function main(x) {
    var alertstring = '';
    if(x.description.value =='')
    alertstring = 'you have not entered in a description.';
    if(x.awarddate.value ==''){
    alertstring += '\nyou have not entered in an award date.';

    if (alertstring != ''){
    alert(alertstring + '\nI need both fields filled!');
    return false;
    }
    return true;
    };

    </script>

    </head>
    <body>

    <form action="submit.html" method="post"
    onsubmit="return main(this)">

    Description: <input name="description">
    <br>
    Award Date: <input name="awarddate">
    <br>
    <input type="submit" value="Submit me">
    </form>

    </body>
    </html>
     
    Evertjan., Oct 24, 2005
    #3
  4. Rick

    Rick Guest

    Thanks everybody,

    Code works great now!

    Rick
     
    Rick, Oct 24, 2005
    #4
  5. Rick

    Rick Guest

    Thanks everybody,

    Code works great now!

    Rick
     
    Rick, Oct 24, 2005
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.