I have a form with one textbox and one button. Here I need to validate if users are not entering invalid data in the textbox so I have a function in site.masters “head” which is called “onclick” event. But I get an error:
“Microsoft JScript runtime error: ‘document.Form1.studentID’ is null
or not an object”
Here “Form1” is the “id” of the form
Here is my javascript code from Site.Master:
<script language="javascript">
function verifyInput() {
if (document.Form1.studentID.value != "testID")
{
alert("please enter valid student ID")
}
}
</script>
Here is my form code from my view:
<form id="Form1" method="get" action="/AddStudent/" runat="server">
<label for="id">
<br /><br /> Student ID:
</label>
<input type="text" name="studentID" maxlength=10/>
<input type="submit" value="Add Student" onclick="verifyInput()"/>
</form>
I believe it needs to be
but really you should be using document.getElementById()
edit: as stated in the answer, yes it should be a name, not an ID, unless you use document.getElementById instead of the old fashioned dot notation