Reputation: 1
Does anyone know why this javascript isn't working ? Its returning the field is empty when its not
The html i'm using on the form is:
onsubmit="return validate_form ( );" method="post" name="AddPTR"
And the javascript is:
<script type="text/javascript">
function validate_form ( )
{
valid = true;
if ( document.forms.AddPTR.PTR2.value == "" )
{
alert ( "Please fill in the PTR box." );
valid = false;
}
return valid;
}
</script>
Upvotes: 0
Views: 91
Reputation: 27880
document.forms
is a standard DOM property that holds an array with the <form>
tags in the document.
If you want to access the AddPTR
form, you can do it in two ways:
<form>
in the document, or know the order they are in, you can access the array by index: document.forms[0]
.<form>
by its name AddPTR
, you can get it from the document.forms
property accessing it as an associative array: document.forms["AddPTR"]
. This approach has the advantage of being independent from the document layout.See this W3C link for some information on how to access associative arrays: Objects in JavaScript: Objects as associative arrays
Upvotes: 5