Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2008
    Location
    Co.Tyrone, Ireland
    Posts
    80
    Thanks
    8
    Thanked 1 Time in 1 Post

    Simple Validation

    Hi everyone,

    I am using this code to validate a form with 3 questions each question needs to be correct for the complete form to process.

    Could someone please look at this script and tell me why its not working?

    Many thanks
    Donno

    Code:
    <script language="JavaScript">
    function checkform()
    {
        if (Question1.value == 'Correct Answer')
        {
            return false;
        }
        else if (Question2.value == 'Correct Answer')
        {
            return false;
        }
        else if (Question3.value == 'Correct Answer')
        {
            return false;
        }
    
        return true;
    }
    </script>

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    You should append document and the name of your form.
    Code:
    if (document.formName.Question1.value == 'Correct Answer')
    If that ain't help, show us your markups (HTML).
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Mar 2008
    Location
    Co.Tyrone, Ireland
    Posts
    80
    Thanks
    8
    Thanked 1 Time in 1 Post
    I have cut peices out as these are the only feilds I require to be validated.

    Thanks
    Donno
    JAVASCRIPT
    Code:
    function checkform()
    {
        if (document.submitter.Question1.value == 'Corrent Answer')
       {
            return false;
       } 
        else if (document.submitter.Question2.value == 'Corrent Answer')
       {
            return false;
       } 
        else if (document.submitter.Question3.value == 'Corrent Answer')
        {
            return false;
        } 
    
        return true;
    }

    HTML
    Code:
     
    <form method="post" action="" onsubmit="return checkform()" name="submitter">
    <table border="0" class="formPage">
    
    <tr>
          <td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
             <td class="field">
                <input type="text" name="question1" size="40" maxlength="255" class="text" />
                <br />
                </td>
       </tr>
             <tr>
          <td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
             <td class="field">
                <input type="text" name="question2" size="40" maxlength="255" class="text" />
                <br /></td>
       </tr>
             <tr>
          <td class="label"><span class='req'>*</span>{l}QUESTION???{/l}:</td>
             <td class="field">
                <input type="text" name="question3" size="40" maxlength="255" class="text" />
                <br /></td>
       </tr>
    <td colspan="2" class="buttons"><input type="submit" name="submit" value="{l}Continue{/l}" class="btn" /></td>
       </tr>
    </table>
    </form>
    Last edited by Donno24; 06-19-2008 at 12:12 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Donno24 View Post
    I have cut peices out as these are the only feilds I require to be validated.
    The rule of spelling is i before e, except after c.

    and to be really pedantic - append means add after, prepend means add before.

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    JS is case-sensitive. Capitalization matters.
    Code:
    Question1.value
    Question2.value
    Question3.value
    Highlighted should be in small letters.

    BTW, language is a deprecated attribute use:
    Code:
    type="text/javascript"
    Hope it helps.
    Last edited by rangana; 06-19-2008 at 12:42 PM. Reason: Spelling matters too.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •