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

Link to comment
Share on other sites

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.