faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 3 people (33%) answered Yes
Recently 1 of 3 people (33%) answered Yes

Entry

How do I limit a text input to only allow a person to put in values in increments of 1,000 or 2,000?

Nov 19th, 2004 12:28
Ky Dang, Judd Payne, Russ Locke,


First you may want to clarify what you are looking for with an 
example... it seems you may be able to accomplish your task with 
either a pre populated dropdown, you could also just do a check on the 
form submit to see if the value is valid. If you want to get into 
events you could possibly do onKeyDown event handling in the text area 
and as a person types, lets say "1", you append the "000" or onBlur 
event you could validate the value and display an alert.
Thanks for responding Russ - :o)
I'm actually doing this within an ecommerce system - I have a text 
field for QTY, and I need it to remain a text field, not a select - but 
for 3 products within the store, and only these 3 products, I need 
people to only be able to order in multiples of 1,000, and on another 
product, in multiples of 2,000.  I've written an if/then statement that 
can handle displaying 2 different syntax's of the <input type=text> but 
I just need to know what javascript I could now use on my text input 
that would only allow them to type in a certain set of numbers - like 
1000, 2000, 3000, 4000, 5000, etc. and then another in multiples of 
2,000.
Make sense?  Any ideas?
----------------------------
Nov. 19, 2004 12:30 PT by Ky
----------------------------
I am not exactly sure what you are talking about but below is my 
interpretation of what you are trying to do.  Basically, I found the 
modulo=0 of the qty interval you want for item1 & item2.  (You did not 
go in detail of item three so I made it detect an even qty.)
Let us know if it works for you.  Thanks.
------ Create an HTML file with the following code example ------
<html>
<script>
function chkInput()
{
	if (document.myForm.T1.value=="widget1")
	{
		if ( (document.myForm.T2.value % 1000) != 0)
		{ alert("please enter a qty of 1000, 2000, etc."); }
	}
	if (document.myForm.T1.value=="widget2")
	{
		if ( (document.myForm.T2.value % 2000) != 0)
		{ alert("please enter a qty of 2000, 4000, etc."); }
	}
	if (document.myForm.T1.value=="widget3")
	{
		if ( (document.myForm.T2.value % 2) != 0)
		{ alert("please enter an even qty" ); }
	}
}
</script>
<body>
Item Part#:<br>
 widget1: has to be in multiples of 1000 qty.<br>
 widget2: has to be in multiples of 2000 qty.<br>
 widget3: any even number<br>
<form name="myForm" method="POST" action="">
<input type="text" name="T1" value="widget1">
<input type="text" name="T2" value="some qty">
<input type="button" value="add2cart" onclick="chkInput();">
</form>
</body>
</html>