Jump to content

Javascript Validation


mckeegan375

Recommended Posts

Hi Guys

 

Was hoping someone could help me out with a (simple?) javascript validation i ahve on my form. I have a similar thing working on another form so cant understand why this isnt working.

 

within the <head> tag i have:

 

<script type="text/javascript">

function validateForm(newcat) {

if(document.newcat.category.selectedIndex == 0) {

alert("Please select a category");

document.forms.newcat.category.focus();

return false;

}

}

</script>

 

My form starts with:

<form name="newcat" method="POST" action="<?php echo $PHP_SELF ?>" onSubmit="return validateForm(newcat);">

 

and there is a dropdown list called "category" within the form. Is it just something simple that im missing?

 

Any help would be greatly appreciated.

 

Cheers

Andy

Link to comment
https://forums.phpfreaks.com/topic/223515-javascript-validation/
Share on other sites

onSubmit="return validateForm(newcat);"

 

Here you're trying to pass a variable called "newcat" that doesn't exist. To pass a string you'd need to have quotes around it:

 

onSubmit="return validateForm('newcat');"

 

However it's easier to pass a special variable called "this", which represents the element's object, and use that within the function. So intead of calling document.newcat.category.selectedIndex for example, you'd use newcat.category.selectedIndex.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.