Javascript Form Validation Issue

Discussion in 'Javascript' started by Nigel, Aug 16, 2006.

  1. Nigel

    Nigel Guest

    Hello,

    I am creating a Perl CGI page with Javascript which I believe has a
    problem with the javascript.

    Below is my form which has two submit buttons with two different
    actions.
    I am having an issue with my "Submit FAQ" button. The "Submit FAQ"
    button calls the javascript function validate(). This function is
    supposed to check to make sure the 'content' field is populated. If
    the field is populated it will go to the action page called
    "addFAQAction.pl". If the 'content' field in undefined a message will
    be displayed to the user stating to populate the 'content' field.
    Currently the functionality does not catch when the 'content' field is
    blank and just continues to the action page.

    Can anyone see what I might be doing wrong which causes my form not to
    catch when 'content' is undefined?

    Thanks in Advance.
    Nigel


    #!/usr/bin/perl -w
    use strict;
    use CGI;
    my $content = param('content');

    print <<"HTML";
    <HTML>
    <HEAD>
    <SCRIPT language='javascript'>

    function checkContent (strng) {
    var error = "";
    if (strng == ""){
    error = "Please enter some content for this FAQ.\n";
    }
    return error;
    }


    function validate()
    {
    var why = "";
    why += checkContent(addFAQ.content.value);
    if (why != ""){
    alert(why);
    return false;
    }
    else{
    document.addFAQ.submit();
    return true;
    }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <form name='addFAQ' id='addFAQ' action='addFAQAction.pl' method='post'>
    <table>
    <tr>
    <td>FAQ Content:<br></td>
    HTML
    <td><textarea id='content' name='content' cols='70'
    rows='20'>$content</textarea></td>
    print <<"HTML";
    </tr>
    <tr>
    <td></td>
    <td>
    <input type="submit" name="update" value="Update Page"
    onClick=\"document.addFAQ.action='addFAQ.pl'\">
    <input type="submit" name="Submit" value="Submit FAQ"
    onSubmit='javascript:validate();'>
    </td>
    </tr>
    </table>
    </form>
    </BODY>
    </HTML>
    HTML
     
    Nigel, Aug 16, 2006
    #1
    1. Advertising

  2. Nigel

    web.dev Guest

    Nigel wrote:
    > <SCRIPT language='javascript'>


    The language attribute is deprecated, use the type attribute instead:

    <script type = "text/javascript">

    > <form name='addFAQ' id='addFAQ' action='addFAQAction.pl' method='post'>

    [snip]
    > <input type="submit" name="update" value="Update Page"
    > onClick=\"document.addFAQ.action='addFAQ.pl'\">
    > <input type="submit" name="Submit" value="Submit FAQ"
    > onSubmit='javascript:validate();'>


    1. When you "update the page", you are still submitting the page.
    2. The input element does not have an onsubmit event handler, the form
    element does.

    Personally, I discourage the use of multiple submit buttons, as it
    usually seems unnecessary. You could for example, use a checkbox to
    indicate you're "updating a page" instead.
     
    web.dev, Aug 16, 2006
    #2
    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. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    429
    Tommy
    Nov 29, 2003
  2. Grigory Temchenko
    Replies:
    0
    Views:
    332
    Grigory Temchenko
    Dec 28, 2008
  3. Replies:
    7
    Views:
    633
  4. bnp
    Replies:
    4
    Views:
    325
  5. francisco lopez
    Replies:
    2
    Views:
    266
    Hywel Jenkins
    Dec 30, 2004
Loading...

Share This Page