emma57573 Posted July 12, 2008 Share Posted July 12, 2008 My javascript knowledge is limited but Im not sure why this isnt working. <script type="text/javascript"> <!-- function validate(form) { if(form.product_name.value == "") { alert('Please specify Item Title'); form.product_name.focus(); return false; } if(form.product_name.value.match(/[&<>]+/)) { alert("Please remove invalid characters from Item Title i.e. & < >"); form.product_name.focus(); return(false); } if(form.cat1.value == "") { alert('Please choose a category'); //form.cat1.focus(); return false; } if(form.rte1.value == "") { alert('Please specify Item Description'); return false; } if(form.buy_price.value!="") { if(isNaN(form.buy_price.value) || (form.buy_price.value<=0)) { alert('Please specify positive value for items Price'); form.buy_price.focus(); return false; } } if(form.shipping_price.value=="" || isNaN(form.shipping_price.value) || form.shipping_price.value<0) { form.shipping_price.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.combine_shipping_price.value=="" || isNaN(form.combine_shipping_price.value) || form.combine_shipping_price.value<0) { form.combine_shipping_price.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.worldwide.checked==yes) { if(form.worldwide_shipping.value=="" || isNaN(form.worldwide_shipping.value) || form.worldwide_shipping.value<0) { form.worldwide_shipping.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.worldwide_combine_shipping.value=="" || isNaN(form.worldwide_combine_shipping.value) || form.worldwide_combine_shipping.value<0) { form.worldwide_combine_shipping.focus(); alert('Please specify combined Overseas Postage Costs'); return false; } } //--> </script> <form name="form" onSubmit="return Validate(this);" action="insert_product.php?user_id=<? echo $_REQUEST["user_id"] ?>" method="post"> The form just posts without validating :-\ Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 12, 2008 Share Posted July 12, 2008 well...a couple things. first, your javascript function is validate(), but in the onsubmit, you call Validate(). also, technically, onsubmit should be all lower case (but that isn't your problem). the updated form tag should be: <form name="form" onsubmit="return validate(this);" action="insert_product.php?user_id=<? echo $_REQUEST["user_id"] ?>" method="post"> also, in your javascript, you were missing a close brace at the very end. focus more on your indentation, it helps find little stuff like that: <script type="text/javascript"> <!-- function validate(form) { if(form.product_name.value == "") { alert('Please specify Item Title'); form.product_name.focus(); return false; } if(form.product_name.value.match(/[&<>]+/)) { alert("Please remove invalid characters from Item Title i.e. & < >"); form.product_name.focus(); return false; } if(form.cat1.value == "") { alert('Please choose a category'); //form.cat1.focus(); return false; } if(form.rte1.value == "") { alert('Please specify Item Description'); return false; } if(form.buy_price.value!="") { if(isNaN(form.buy_price.value) || (form.buy_price.value<=0)) { alert('Please specify positive value for items Price'); form.buy_price.focus(); return false; } } if(form.shipping_price.value=="" || isNaN(form.shipping_price.value) || form.shipping_price.value<0) { form.shipping_price.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.combine_shipping_price.value=="" || isNaN(form.combine_shipping_price.value) || form.combine_shipping_price.value<0) { form.combine_shipping_price.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.worldwide.checked==yes) { if(form.worldwide_shipping.value=="" || isNaN(form.worldwide_shipping.value) || form.worldwide_shipping.value<0) { form.worldwide_shipping.focus(); alert('Please specify Overseas Postage Costs'); return false; } if(form.worldwide_combine_shipping.value=="" || isNaN(form.worldwide_combine_shipping.value) || form.worldwide_combine_shipping.value<0) { form.worldwide_combine_shipping.focus(); alert('Please specify combined Overseas Postage Costs'); return false; } } } //--> </script> final note...if there is any error in your JS function, the false will never be returned, and the form will be submitted. so make sure your JS is solid. a good way to test it is to add a button to test the validation (where the form isn't actually submitted) <input type="button" value="Validate" onclick="validate(document.forms['form']);" /> Quote Link to comment Share on other sites More sharing options...
emma57573 Posted July 16, 2008 Author Share Posted July 16, 2008 Thank you sorry I think I got over tired at that point last week Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.