Page 1 of 1

onChange Validation

Posted: Fri Dec 30, 2005 1:42 am
by phppick
Hi,

I have a form like this

Code: Select all

function ValidateMe(thisform) {

	if(thisform.Price.value==""){
		alert("Enter Price");
		thisform.Price.focus();
		return false;		
	}	
	if(thisform.Quantity.value==""){
		alert("Enter Quantity");
		thisform.Quantity.focus();
		return false;		
	}	
	if(thisform.Tag.value==""){
		alert("Enter Tag");
		thisform.Tag.focus();
		return false;		
	}	

	thisform.submit(); // this line submits the form after validation
}

<form name="frm1" action="" method="post">
Quantity:<input name=Quantity>
Price:      <input name=Price>
Tag:        <input name="Tag" onChange="ValidateMe(frm1);return false;this.form.submit()">
</form>

<form name="frm2" action="AddValues.php" method="post">
<!-- Other Input Controls and in this place i will submit the form to next page-->
</form>
So, in the first Form Area, i want to validate the user should enter Quantity & Price and Tag, before coming to next Form Area. i want this is happen onChange or some other event of input control 'Tag'. Becuase i will prefix Quantity to Tag.

i am able to validate onChange of Tag, but when Quantity & Price entered, the form should submit.

Any Ideas pls le me know.

Tq

Posted: Fri Dec 30, 2005 4:24 am
by foobar
Why don't you check both Quantity and Price for input with the onChange event, and at the same time check if the other is already set.

Like so (pseudocode):

Code: Select all

quantity.onchange = is_set(price) ? "yes" : "no";
price.onchange = is_set(quantity) ? "yes" : "no"';
Wherever there is a "yes", submit your form, and wherever there is a "no", you display an error message.