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 to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy JavaScript Validation Error.

    I have a code with me, and everything works fine (JavaScript) excluding the validation; I think I may have made an error somewhere which could possibly relate to the curly braces I've used. I was wondering if anybody could correct this code. I'm using Notepad++, if that helps.

    Code:
    <head>
    <title>Exam entry</title>
    
    <script language="javascript" type="text/javascript">
    
    function validateForm() {
    var result = true;
    var msg="";
    }
    
    if (document.ExamEntry.name.value=="") {
    msg+="You must enter your name \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    result = false;
    }
    
    if (document.ExamEntry.subject.value=="") {
    msg+="You must enter the subject \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    result = false;
    }
    
    if (document.ExamEntry.examination number.value="") {
    msg+="You must enter the examination number \n";
    document.ExamEntry.examinationnumber.focus();
    document.getElementById('examinationnumber').style.color="red";
    result = false;
    }
    
    if (msg=="") {
    return result;
    }
    
    {
    window.alert(msg)
    return result;
    }
    </script>
    </head>
    <body>
    <p>
    <font face="Candara">
    <font size="2">
    <h1><u>Exam Entry Form</u></h1>
    <fieldset>
    <form name="ExamEntry" method="post" action="success.html">
    <table width="50%" border="0">
    <tr>
    <td id="name">Name</td>
    <td><input type="text" name="name" /></td>
    </tr>
    <tr>
    <td id="subject">Subject</td>
    <td><input type="text" name="subject" /></td>
    </tr>
    <tr>
    <tr>
    <td id="examination number">Examination Number</td>
    <td><input type="text" name="examinationnumber" /></td>
    </tr>
    <tr>
    </tr>
    <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
    </tr>
    </fieldset>
    </table>
    </form>
    </p></font></size></body>
    Thanks in advance.
    Last edited by Coding123; 12-17-2012 at 10:40 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Have you tried using your error console?

    function validateForm() {
    var result = true;
    var msg="";
    } // end of function

    And other errors, including obsolete HTML <font> tags and no !DOCTYPE.

    Form validation of the pattern if (document.formname.formfield.value == "") - that is blank - is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. A proper name may only contain letters, hyphen, space and apostrophe.
    Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 12-17-2012 at 11:14 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Jul 2011
    Location
    India
    Posts
    496
    Thanks
    3
    Thanked 57 Times in 56 Posts
    Post ur full code or link where it is hosted.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by vikram1vicky View Post
    Post ur full code or link where it is hosted.
    He has posted his full code.What do you think is missing (apart from <html> and </html>)?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The code looks fine except for the </size> tag. Netscape 2 doesn't understand doctype tags so with the rest of the code written for that browser it is essential then a doctype not be specified.

    If targetting more recent browsers such as IE5 the first thing you should do is to rewrite the HTML to a more recent version as the version being used became obsolete in 1997.

    If you want it to work properly in modern browsers you should also move the script to just before the </body> tag and get rid of the alert() call as they can no longer be used in live web pages. You should also learn how to use the debugger built into the browser to work out what your JavaScript errors are - as well as feeding your JavaScript through a service such as http://www.jslint.com/ (created by JavaScript expert Douglas Crockford).
    Last edited by felgall; 12-22-2012 at 08:20 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

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