Results 1 to 2 of 2

Thread: Help id the error in my code

  1. #1
    Join Date
    Nov 2010

    Help id the error in my code

    Working on a health form and I cant seem to get the javascript code to work out for me. Im trying to get the a message to pop up that says invalid "whatever" if there is nothing entered in the field or if they entered a field incorrectly (such as letters in a field that requires only numbers and). I cant find where the error is in my javascript code though. If anyone can help me identify the errors I would greatly appreciate it.

    <html lang="en">
        <link rel="stylesheet" href="Progect.css" type="text/css" />
        <title>Research Form</title>
        <form action="http://www.turtle.cs.umd.edu/classes/122/researchFormAssignment/researchProcessing.php " method="get" onsubmit="return validateForm()">
                            <legend><em><strong><FONT SIZE=5>General Information</FONT SIZE></strong></em></legend>
                    Firstname:<input id="firstName" type="text" name="firstname" /> Lastname:<input id="lastName" type="text" name="lastname" /> <br /> <br />
                                    Phone: <input id="firstThreeNumbers" type="text" name="phoneFirstPart" size="3" maxlength="3" />
                                    -<input type="text" name="phoneSecondPart" size="3" maxlength="3" />
                                    -<input type="text" name="phoneThirdPart" size="4" maxlength="4" />
                                    Email: <input id="email" type="text" name="email" size="60" maxlength="100" /><br />
                                <legend><em><strong><FONT SIZE=5>Research Study Data</FONT SIZE></strong></em></legend>
                                    Age:<input id="age" type="text" name="age" size="3" maxlength="3" /> <br /> <br />
                                    Height:<input id="height" type="text" name="heightFeet" size="1" maxlength="1" />
                                    Feet<input type="text" name="heightInches" size="2" maxlength="2" /> Inches <br /> <br />
                                    Weight:<input id="weight" type="text" name="weight" size="3" maxlength="3" /> pounds
                                    Which of the following conditions are present in your famile? (check all the apply) <br /> <br />
                                    <input type="checkbox" id="bloodpressure" name="highBloodPressure" value="bloodpressure" />High Blood Pressure
                                    <input type="checkbox" id="diabetes" name="diabetes" value="diabetes" />Diabetes
                                    <input type="checkbox" id="glaucoma" name="glaucoma" value="glaucoma" />Glaucoma
                                    <input type="checkbox" id="asthma" name="asthma" value="asthma" />Asthma
                                    <input type="checkbox" id="none" name="none" value="none" />None
                                    <legend>Time Period</legend>
                                    How long have you experienced any of the above conditions? <br /> <br />
                                    <input type="radio" id="radioNever" name="period" value="never" />Never
                                    <input type="radio" id="radioLessThanAYear" name="period" value="less" />Less than a year
                                    <input type="radio" id="radioOneToTwoYears" name="period" value="OneToTwoYears" />One to two years
                                    <input type="radio" id="radioMoreThanTwoYears" name="period" value="more" />More than two years<br />
                                    <legend>Study Information</legend>
                                    Which study are you taking part of?<br /> <br />
                                    <select id="studyInfo" name="studyType">
                    <option value="shortTerm">Short Term</option>
                                    <option value="longTerm">Long Term</option>
                                    <br /> <br />Assigned Study Id:<input id="studyID' type="text" name="firstFourDigits" size="4" maxlength="4" />
                                    -<input type="text" name="secondFourDigets" size="4" maxlength="4" />
                                    <legend>Additional Information (Comments)</legend>
                                    <textarea id="request" name="comments" rows="8" cols="100">
                            <input type="reset" />
                            <input type="submit" />
       <script type="text/javascript">
                        function validateForm() {
                /* Retrieving the values */
                            var firstName = document.getElementById("firstName").value;
                            var lastName = document.getElementById("lastName").value;
                            var firstThreeNumbers = document.getElementById("firstThreeNumbers").value;
                            var email = document.getElementById("email").value;
                            var age = document.getElementById("age").value;
                            var height = document.getElementById("height").value;
                            var weight = document.getElementById("weight").value;
                            var studyID = document.getElementById("studyID").value;
                /* Validating numeric values */    
                var invalidMessages = "";
                            if (String(parseInt(firstThreeNumbers)) !== firstThreeNumbers) {
                                invalidMessages += "Invalid Phone Number provided.\n";
                             if (String(parseInt(age)) !== age) {
                                invalidMessages += "Invalid Age provided.\n";
                            if (String(parseInt(height)) !== height) {
                                invalidMessages += "Invalid Height provided.\n";
                            if (String(parseInt(weight)) !== weight) {
                                invalidMessages += "Invalid Weight provided.\n";
                if (invalidMessages !== "") {
                    return false;
                            }  else {
                    var valuesProvided = "Do you want to submit the following form?\n";
                                    if (window.confirm(valuesProvided))
                        return true;
                        return false;
        <!-- Insert your content here -->

  2. #2
    Join Date
    Mar 2007
    Use the FireFox error console and if you still don't see the problem, validate the page. http://validator.w3.org
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Recent Articles