Submit Form After Certain Character Length [SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Submit Form After Certain Character Length [SOLVED]

Post by Tomcat7194 »

Hello. I am trying to find/write a script that will submit a form automatically after a certain number of characters (10) have been entered into a text field. I don't want to have to involve a button or anything like that--as soon as the characters are entered, the form needs to submit. I assume that I'll have to use Javascript or something along those lines, possibly with something like

Code: Select all

if(document.myform.onsubmit())
 {
 document.myform.submit();
 }
but I'm not sure how to trigger that based on the number of characters entered. Any ideas?

Thanks
Tom
Last edited by Tomcat7194 on Sun May 13, 2007 5:09 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You would probably add a onkeypress, onkeydown, or onkeyup event to the text/textarea input that calls a Javascript function. That function would check the length of the input's value and submit the form if it was over a certain length.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sounds incredibly annoying to me. I hope you're not making customers use this broken design ;) Oops, I made a typo, lemme backsp... sh**, where did the form go? :x Hmm, I'll go to another site instead.

;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Amen d11.
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

Nah, actual users will never encounter it. :D

I will be using it to query a database and obtain data based on a number that will be entered using a barcode scanner. The scanner acts like a keyboard, entering the numbers into whatever form is selected. However, I don't want to have to press the enter key every time a number is scanned, so I want it to initiate the query whenever a 10 digit number (ISBN, in this case) is entered into the form.

Tom
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Code: Select all

<script type="text/javascript">
function checkLength(field) {
	if (field.value.length == 9)
	{
		document.myform.submit();
	}
}
</script>

<form name="myform" action="nowhere.html" method="post">
<input type="text" name="code" onKeyDown="javascript:checkLength(this)" />
</form>
I'm feeling nice/bored :)
Tomcat7194
Forum Commoner
Posts: 48
Joined: Mon Jul 31, 2006 1:34 pm

Post by Tomcat7194 »

Thanks, I just got it working a moment before you posted using some cobbled-together code

Code: Select all

<script language="JavaScript">
function update() {
   if (document.f.box.value.length > 9) {
   document.f.submit();
   }
   }
   </script>
<form method="POST" action="getinfo.php" name="f">
<textarea rows="12" name="box" cols="60" wrap="virtual" onkeyup="update();"></textarea>
<form action="submit">
</form>
Rather similar, except for the == and the keydown rather than keyup :D

Tom
Post Reply