Hey all I need to use java script function to validate fields of two form.
Basically the criteria is when user select form name then the require fields has to be visible on next form.. I need to ask that if I can use java script function for forms on different pages. Below is my code
code for form rpt
<html>
<head>
<script type="text/javascript" src="javascript/formvalidations.js"></script>
</head>
<form name="rpt" method="post" action="">
<label>Form Name </label>
<select id="formname" name="formname">
<option value="" selected="selected">--Please Select Form Name--</option>
<option value="1">1</option>
<option value="2">2</option>
</select><br />
<input id="ok" type="submit" name="ok" value="OK" onclick="return chkform();" />
</form>
</html>
code for java script function
function chkform(){
var formname = document.forms.rpt.formname.value;
if (formname == "1"){
document.forms.reportform.getElementById("1").style.display="inline";
document.forms.reportform.getElementById("2").style.display="inline";
document.forms.reportform.getElementById("3").style.display="inline"
document.forms.reportform.getElementById("4").style.display="none";
document.forms.reportform.getElementById("5").style.display="none";
}
elseif (formname == "2"){
document.forms.reportform.getElementById("1").style.display="inline";
document.forms.reportform.getElementById("2").style.display="none";
document.forms.reportform.getElementById("3").style.display="inline"
document.forms.reportform.getElementById("4").style.display="none";
document.forms.reportform.getElementById("5").style.display="inline";
}
else{
alert("select form name");
return false;
}
}
I have 5 fields in form reportform. and I need required fields to be visible in reportform. lets say if user select value 1 in rpt then field 1, 2 and 3 has to be visible.
You’ve got a syntax error on this line:
JavaScript doesn’t have
elseifas a single word, you need to insert a space:Also, the
getElementById()function is a method ofdocument, not of individual html elements, so use:If you use your browser’s JS console (e.g., in Chrome press F12) it will report these errors to you (it won’t tell you how to fix them, but at least it’ll point you in the right direction).