Liam Bishop
Liam Bishop

Reputation: 1

Html/javascript validation failing

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

Answers (1)

Xavi L&#243;pez
Xavi L&#243;pez

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:

  • If you've only got one <form> in the document, or know the order they are in, you can access the array by index: document.forms[0].
  • If you want to get the <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

Related Questions