Page 1 of 1
Submit Form After Certain Character Length [SOLVED]
Posted: Sun May 13, 2007 11:22 am
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
Posted: Sun May 13, 2007 12:36 pm
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.
Posted: Sun May 13, 2007 1:33 pm
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?

Hmm, I'll go to another site instead.

Posted: Sun May 13, 2007 1:56 pm
by John Cartwright
Amen d11.
Posted: Sun May 13, 2007 4:39 pm
by Tomcat7194
Nah, actual users will never encounter it.
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
Posted: Sun May 13, 2007 4:59 pm
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

Posted: Sun May 13, 2007 5:11 pm
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
Tom