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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Form Help!

    I've created a JavaScript validation form that should produce errors when the information isn't correctly filled out. The issue is that it's not producing any errors. When the button "Check details" is clicked, it goes directly to my second HTML page no matter if the form is filled or not. I can't seem to find the problem, as I have gone over the code many times. I just need the errors fixed, I can't change the code, as this is what is required for my class. If you can help me find what is wrong with my code, I would greatly appreciate it!

    Code:
     <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function validateform(){
    var ageErr = document.getElementById('ageErr');
    var nameErr = document.getElementById('nameErr');
    var results = document.getElementById('results');
    var theName = document.form1.theName;
    var theAge = document.form1.theAge;
    var theRadioGp = document.form1.gender;
    var chkValue;
    if(theName.value =="") {
    	nameErr.innerHTML = 'Please Insert Your Name';
    	theName.focus();
    	return false; 
    } else {
    	nameErr.innerHTML ="";
    }
    if (theAge.value =="") {
    	ageErr.innerHTML = 'An age is required';
    	theAge.focus();
    	return false;
    } else if (isNaN(theAge.value)) {
    	ageErr.innerHTML ='Age should be a number';
    	theAge.select();
    	return false;
    } else if (theAge.value <18 || theAge.value > 100) {
    	ageErr.innerHTML ='Age can not be less than 18';
    	theAge.select();
    	return false;
    } else {
    	ageErr.innerHTML ="";
    		}
    for(var i=0; i < theRadioGp.length; i++) {
    	if (theRadioGp[i].checked == true) {
    	chkValue = theRadioGp[i].value;
    	}
    }
    if (chkValue == undefined){
    	alert('Please select Gender');
    	return false;
    }
    }
    </script>	
    </head>
    			
    <body>
    	<h2>Form Validation<h2><hr />
    	<p> Please Fill out the Form below: </p>
    	
    <form action ="thankyou.html" id="form1" method="post" onsubmit="return validateForm()"> 
    
    	<p>Name:</br>	
    		<input type="text" name="theName"><span class="err" id="nameErr"></span>
    	</p>
    
    	<p> Age: <br/>
    		<input type="text" name="theAge"><span class="err" id="ageErr"></span>
    	</p>
    	
    	<p> Gender </p>
    		<input type="radio" name="gender" value="male">Male<br>
    		<input type="radio" name="gender" value="female">Female<br> 
    	</p>
    	
    	<p> <input type="submit" value="Check Details"></p>
    	</form>
    	<span id="results"></span>
    </body>	
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    (1) Look VERY closely:
    Code:
    function validateform(){
    ...
    <form action ="thankyou.html" id="form1" method="post" onsubmit="return validateForm()">
    JavaScript is CASE SENSITIVE! NEVER forget that.

    (2) This method of addressing a <form>:
    Code:
    var theName = document.form1.theName;
    requires that the NAME, not the ID, of the <form> be used. Your <form> does not have a name.

    You could fix it by adding a name= to the <form>, but more modern HTML deprecates named forms, so better would be to use
    Code:
    var theName = document.getElementById("form1").theName;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the advice! I took your advice, and still can't seem to get it to work! It's almost as if the innerHTML isn't working. I'm not receiving any errors in the JavaScript Console on chrome; but I keep getting an syntax error in DreamWeaver for the line that contains [code] var ageError = document.getElementById('ageError'); [code]. Here is my edited code per your recommendations.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    function validateform(){
    var ageError = document.getElementById('ageError');
    var nameError = document.getElementById('nameError');
    var results = document.getElementById('results');
    var theName = document.getElementById('form1').theName;
    var theAge = document.form1.theAge;
    var theRadioGp = document.form1.gender;
    var chkValue;
    if(theName.value =="") {
    	nameError.innerHTML = 'Please Insert Your Name';
    	theName.focus();
    	return false; 
    } else {
    	nameError.innerHTML ="";
    }
    if (theAge.value =="") {
    	ageError.innerHTML = 'An age is required';
    	theAge.focus();
    	return false;
    } else if (isNaN(theAge.value)) {
    	ageError.innerHTML ='Age should be a number';
    	theAge.select();
    	return false;
    } else if (theAge.value <18 || theAge.value > 100) {
    	ageError.innerHTML ='Age can not be less than 18';
    	theAge.select();
    	return false;
    } else {
    	ageError.innerHTML ="";
    		}
    for(var i=0; i < theRadioGp.length; i++) {
    	if (theRadioGp[i].checked == true) {
    	chkValue = theRadioGp[i].value;
    	}
    }
    if (chkValue == undefined){
    	alert('Please select Gender');
    	return false;
    }
    }
    </script>	
    </head>
    			
    <body>
    	<h2>Form Validation<h2><hr />
    	<p> Please Fill out the Form below: </p>
    	
    <form action ="thankyou.html" id="form1" method="post" onsubmit="return validateform()"> 
    
    	<p>Name:</br>	
    		<input type="text" name="theName"><span class="err" id="nameErr"></span>
    	</p>
    
    	<p> Age: <br/>
    		<input type="text" name="theAge"><span class="err" id="ageErr"></span>
    	</p>
    	
    	<p> Gender </p>
    		<input type="radio" name="gender" value="male">Male<br>
    		<input type="radio" name="gender" value="female">Female<br> 
    	</p>
    	
    	<p> <input type="submit" value="Check Details"></p>
    	</form>
    	<span id="results"></span>
    </body>	
    </html>

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    READ READ READ what I wrote. You have *NOT* fixed problem (2), at all! You have not changed a *THING* in regards to document.form1 !!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    Sorry. I see you changed ONE line, but you need to change *THREE*:
    Code:
    var theName = document.getElementById('form1').theName;
    var theAge = document.form1.theAge;
    var theRadioGp = document.form1.gender;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    A better way to do that:
    Code:
    var form = document.getElementById('form1');
    var theName = form.theName;
    var theAge = form.theAge;
    var theRadioGp = form.gender;
    Why repeat lengthy code?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,643 Times in 4,605 Posts
    Suggestions to improve the usability of the page:
    (1) Give up to all 3 error messages at one time! Your code will only give an error message for the first one found.

    (2) re the radio buttons: alert( ) is considered obsolete. Don't use it. Use an error message same as the other two fields.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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