1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Calling HTML / Javascript boffs

Discussion in 'Software' started by Arghnews, 31 Oct 2011.

  1. Arghnews

    Arghnews What's a Dremel?

    Joined:
    6 Jul 2011
    Posts:
    129
    Likes Received:
    4
    <html>
    <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 here \n";
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    result = false;
    }
    if (document.ExamEntry.subject.value=="") {
    msg+="You must enter the subject here \n";
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    result = false;
    }
    if(msg==""){
    return result;
    }

    {
    alert(msg)
    return result;
    }
    }

    </script>
    </head>

    <body>
    <h1>Exam Entry Form</h1>
    <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>
    <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
    </tr>

    </table>
    </form>
    </body>
    </html>

    Question: what's wrong with this code?
    It's supposed to bring an error message if you don't fill the fields. It doesn't, it always take me straight to the success page. This is the sample code we've been given as a base, and it doesn't work... it really bugs me, either there's meant to be an error or I've mistyped, but I've quad checked and I don't think they'd purposely put an error in? :wallbash:

    Ty for any help
     
  2. Malvolio

    Malvolio .

    Joined:
    14 Dec 2003
    Posts:
    4,632
    Likes Received:
    178
    Guess what? I just learned javascript! Weee! No clue if it'll pass the info through properly, as I've only really had a few minutes of self-induced learning, but it validates properly and seems to work as best I can tell.

    Code:
    <html>
    <head>
    <title>Exam entry</title>
    
    <script type="text/javascript">
    
    function validateForm() {
    
    if (document.ExamEntry.name.value=="")	 {
    document.ExamEntry.name.focus();
    document.getElementById('name').style.color="red";
    alert("You must enter name here!");
    return false;
    }
    else if (document.ExamEntry.subject.value=="") {
    document.ExamEntry.subject.focus();
    document.getElementById('subject').style.color="red";
    alert("You must enter subject here!");
    return false;
    }
    else { return true; }
    }
    
    </script>
    </head>
    
    <body>
    <h1>Exam Entry Form</h1>
    <form name="ExamEntry" method="post" onsubmit="return validateForm()" 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>
    <td><input type="submit" name="Submit" value="Submit" /></td>
    <td><input type="reset" name="Reset" value="Reset" /></td>
    </tr>
    
    </table>
    </form>
    </body>
    </html>
    Enjoy!


    Edit: guess I should point out the errors. The "onsubmit" was incorrectly placed, the if statement wasn't correct, the popup message wasn't an alert (as it should have been), and the order of operations was incorrect. There was also a lot of extra stuff that I couldn't figure out at all! So I just deleted it and put in something else.
     
    Last edited: 31 Oct 2011
    Arghnews likes this.
  3. Arghnews

    Arghnews What's a Dremel?

    Joined:
    6 Jul 2011
    Posts:
    129
    Likes Received:
    4
    Thank you so much

    And yes, the errors at what I'm most interested in, being a novice in JS/HTML, and trying to learn it. so thank you very very much :D
     
  4. getDownShep

    getDownShep What's a Dremel?

    Joined:
    14 Aug 2011
    Posts:
    180
    Likes Received:
    10
    If you have Firefox installed you can get an add on called the web developer toolbar that can help with web based stuff
     
    Arghnews likes this.
  5. Arghnews

    Arghnews What's a Dremel?

    Joined:
    6 Jul 2011
    Posts:
    129
    Likes Received:
    4
    ah, thank you too
     
  6. Zoon

    Zoon Hunting Wabbits since the 80s

    Joined:
    12 Mar 2001
    Posts:
    5,888
    Likes Received:
    824
    Also, Firebug is very useful for Firefox based web-dev. I mainly use it for CSS debugging but it will also very ably do javascript.
     
  7. lp rob1

    lp rob1 Modder

    Joined:
    14 Jun 2010
    Posts:
    1,530
    Likes Received:
    140
    +1 for Firebug. For more advanced web programming *cough*hacking*cough* then Firecookie becomes useful. And before you start bashing me that hacking is illegal, immoral etc. I mean the find-security-vunerabilities type of hacking, not destroy-everything hacking. And anyway, the second one should be called cracking, because it damages a system/data.

    /offtopic
     
    Blazza181 likes this.
  8. Malvolio

    Malvolio .

    Joined:
    14 Dec 2003
    Posts:
    4,632
    Likes Received:
    178
    No problem, Arghnews! I was really bored last night and figured it would be worth a lark to see if I could figure something out, fully expecting not to do much of anything worthwhile.

    For the record I basically just had notepad open on one corner of my screen with code in, firefox taking up the right-hand pane, and the built in error console (CTRL+SHIFT+J) in the empty, remaining space to diagnose it as I slowly learned how javascript worked. I've done a very minor bit of HTML many years ago, but never really dabbled in JS before, so this was a very fun experience for me!
     

Share This Page