Jump to content

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.