mckeegan375 Posted January 5, 2011 Share Posted January 5, 2011 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 More sharing options...
Adam Posted January 6, 2011 Share Posted January 6, 2011 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. Link to comment https://forums.phpfreaks.com/topic/223515-javascript-validation/#findComment-1155636 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.