onChange Validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
phppick
Forum Commoner
Posts: 57
Joined: Thu Aug 14, 2003 5:59 am

onChange Validation

Post 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
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
Post Reply