I added a function, then the rest of the script are not working..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mark roldan
    New Member
    • Sep 2010
    • 3

    I added a function, then the rest of the script are not working..

    Hi, this is a code for form verification... I added the function results() then the entire script is now not working. Need help... *note that if I omit the results() function, other functional parts (disabling other text field) are working...

    Need help thanks...

    P.S.
    the omitted/commented codes were my attempt of trying to debug the program... XD

    Code:
    <html>
    	<head>
    		<title>HTML DOM Exercise</title>
    		<script type="text/javascript">
    			var er=0;
    			var s=0;
    			function tmp()
    				{
    					document.getElementById("perm1").readOnly=true;
    					document.getElementById("perm2").readOnly=true;
    					document.getElementById("perm3").readOnly=true;
    					document.getElementById("perm4").readOnly=true;
    					document.getElementById("perm5").readOnly=true;
    					document.getElementById("perm6").readOnly=true;
    					document.getElementById("temp1").readOnly=false;
    					document.getElementById("temp2").readOnly=false;
    					document.getElementById("temp3").readOnly=false;
    					document.getElementById("temp4").readOnly=false;
    					document.getElementById("temp5").readOnly=false;
    					document.getElementById("temp6").readOnly=false;
    					document.getElementById("temp7").readOnly=false;
    					s=0;
    				}
    			function prm()
    				{
    					document.getElementById("perm1").readOnly=false;
    					document.getElementById("perm2").readOnly=false;
    					document.getElementById("perm3").readOnly=false;
    					document.getElementById("perm4").readOnly=false;
    					document.getElementById("perm5").readOnly=false;
    					document.getElementById("perm6").readOnly=false;
    					document.getElementById("temp1").readOnly=true;
    					document.getElementById("temp2").readOnly=true;
    					document.getElementById("temp3").readOnly=true;
    					document.getElementById("temp4").readOnly=true;
    					document.getElementById("temp5").readOnly=true;
    					document.getElementById("temp6").readOnly=true;
    					document.getElementById("temp7").readOnly=true;
    					s=1;
    				}
    			function results()
    				{
    					//var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
    					if(s===0)
    						{
    							if((isNaN(document.getElementById("temp1").value))
    								{
    								//document.getElementById("temp1").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Home Number should be a Number! <br>"; er=1;
    								 }
    							if(document.getElementById("temp2").value=="" || document.getElementById("temp2").value==null )
    								{
    								//document.getElementById("temp2").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Street is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("temp3").value=="" || document.getElementById("temp3").value==null )
    								{
    								//document.getElementById("temp3").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "City is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("temp4").value=="" || document.getElementById("temp4").value==null)
    								{
    								//document.getElementById("temp4").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "State is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("temp5").value=="" || document.getElementById("temp5").value==null )
    								{
    								//document.getElementById("temp5").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip Code is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("temp6").value=="" || document.getElementById("temp6").value==null )
    								{
    								//document.getElementById("temp6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Street is a Mandatory Field! <br>";er=1;
    								 }
    							else if(isNaN(document.getElementById("temp6").value))
    								{
    								//document.getElementById("temp6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip Code should be a Number! <br>";er=1;
    								 }
    							else if(document.getElementById("temp6").length>5 || document.getElementById("temp6").length<3)
    								{
    								//document.getElementById("temp6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip code should be 3 to 5 digits long! <br>";er=1;
    								 }
    						}
    					else
    						{
    							if(isNaN(document.getElementById("perm1").value)
    								{
    								//document.getElementById("perm1").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Home Number should be a Number! <br>";er=1;
    								 }
    							if(document.getElementById("perm2").value=="" || document.getElementById("perm2").value==null )
    								{
    								//document.getElementById("perm2").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Street is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("perm3").value=="" || document.getElementById("perm3").value==null )
    								{
    								//document.getElementById("perm3").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "City is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("perm4").value=="" || document.getElementById("perm4").value==null )
    								{
    								//document.getElementById("perm4").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "State is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("perm5").value=="" || document.getElementById("perm5").value==null )
    								{
    								//document.getElementById("perm5").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip Code is a Mandatory Field! <br>";er=1;
    								 }
    							if(document.getElementById("perm6").value=="" || document.getElementById("perm6").value==null )
    								{
    								//document.getElementById("perm6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Street is a Mandatory Field! <br>";er=1;
    								 }
    							else if(isNaN(document.getElementById("perm6").value))
    								{
    								//document.getElementById("perm6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip Code should be a Number! <br>";er=1;
    								 }
    							else if(document.getElementById("perm6").length>5 || document.getElementById("perm6").length<3)
    								{
    								//document.getElementById("perm6").style.color:rgb(252,113,105);
    								 document.getElementById("error").innerHTML += "Zip code should be 3 to 5 digits long! <br>";er=1;
    								 }
    							// if(document.getElementById("perm7").value != '' && !document.getElementById("perm6").innerHTML(re)) 
    								{
    								//document.getElementById("perm7").style.color:rgb(252,113,105);
    								// document.getElementById("error").innerHTML += "Invalid Date Format! <br>";er=1;
    								 }
    						}
    					if(er==0)
    						{
    							alert("Succesful!")
    						}
    					else
    						{
    							er=0;
    						}
    					}
    				}
    		</script>
    	</head>
    	<body>
    		<p id="error">&nbsp</p>
    		<form name="Selector">
    			<input type="radio" onClick="prm()" name="add" value="Perm" /> Permanent Address<br />
    			<input type="radio" onClick="tmp()" name="add" value="Temp" /> Temporary Address<br />
    		</form>
    		<div class="perm">
    			<table>
    				<form name="Permanent">
    					<tr><td colspan="4">Permenent Address: &nbsp;</td></tr>
    					<tr><td>House Number</td><td>Street*</td><td>City*</td></tr>
    					<tr><td><INPUT id="perm1" TYPE="text"   NAME="Hnum"></td><td><INPUT id="perm2" TYPE="text"   NAME="Street"></td><td><INPUT id="perm3" TYPE="text"   NAME="City"></td></tr>
    					<tr><td>State*</td><td>Country*</td><td>Zip*</td></tr>
    					<tr><td><INPUT id="perm4" TYPE="text"   NAME="State"></td><td><INPUT id="perm5" TYPE="text"   NAME="Country"></td><td><INPUT id="perm6" TYPE="text"   NAME="Zip"></td></tr>
    				</form>
    			</table>
    		</div>
    		<br><br>
    		<div class="temp">
    			<table>
    				<form name="Temporary">
    					<tr><td colspan="4">Temporary Address: &nbsp;</td>
    					<tr><td>House Number</td><td>Street*</td><td>City*</td><td></td></tr>
    					<tr><td><INPUT id="temp1" TYPE="text"   NAME="Hnum"></td><td><INPUT id="temp2" TYPE="text"   NAME="Street"></td><td><INPUT id="temp3" TYPE="text"   NAME="City"></td><td></td></tr>
    					<tr><td>State*</td><td>Country*</td><td>Zip*</td><td>Date*</td></tr>
    					<tr><td><INPUT id="temp4" TYPE="text"   NAME="State"></td><td><INPUT id="temp5" TYPE="text"   NAME="Country"></td><td><INPUT id="temp6" TYPE="text"   NAME="Zip"></td><td><INPUT id="temp7" TYPE="text"   NAME="Date">&nbsp; mm/dd/yyyy</td></tr>
    				</form>
    			</table>
    		</div>
    		<br><br>
    		<input type="button" onClick="results()" value="Submit"/>
    	</body>
    </html>
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    In ur code line number 44. It must be if(s==0) not if(s===0).
    In Line No 47 add additional closing paranthesis at the end ")"
    Line No 90 add additional closing paranthesis at the end ")"
    Remove the Additional Brace at Line no 145

    Due to this the function was not working.

    Thanks and Regards
    Ramanan Kalirajan
    Last edited by RamananKalirajan; Sep 7 '10, 09:29 AM. Reason: Added the other sources for Error

    Comment

    • mark roldan
      New Member
      • Sep 2010
      • 3

      #3
      still not working...

      EDIT... ok ok... now its working... thanks... i wasted 1 hour just looking for a missing ')'... XD

      Thanks again

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        one note:

        it is a difference in checks when using:

        Code:
        if (s == 0)
        or:

        Code:
        if (s === 0)
        both are working but the second will even do a typecheck (identity operator), which in the shown case would require that the value of s would be a number, while in the first an implicit typecast is made so even '' == 0 or ' ' == 0 would return true. so basically you should need to know what you want to check ... and it shouldn't need have to be a reason for the non-working script in the first place.

        kind regards
        Last edited by gits; Sep 7 '10, 01:07 PM.

        Comment

        Working...