Jump to content

Not sure why my validation isnt working


emma57573

Recommended Posts

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  :-\

Link to comment
https://forums.phpfreaks.com/topic/114460-not-sure-why-my-validation-isnt-working/
Share on other sites

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']);" />

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.