Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-28-2009, 07:36 PM   PM User | #1
edmiston00
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
edmiston00 is an unknown quantity at this point
help with js form validation, check, wait, then submit

Hi,

I'm writing a web-program that takes input on a form from a barcode scanner. The barcode scanner converts the barcode to text and then uses keystrokes to type the text very quickly (< 1s) into input box. There is a chance that someone may key in a barcode by hand as well.

here's what i need:
1. verify form is at least 9 characters (some of my barcodes will be longer), if it is then:
2. wait 2 seconds and submit form
3. but if another keystroke occurs, clear the timer and wait 2 seconds from then (in case someone is typing by hand and isn't finished yet).

I have very basic JS experience and tried to splice two scripts together but I couldn't get it working, here is what I have (dont laugh!):
Code:
<script language="javascript">
function check(field)
{
if ( field.value.length >= 9 )
{
clearTimeout (t); 
var t=setTimeout("field.form.submit()",2000);
}	  
}
</script>
AND THE FORM
Code:
<form method="post" action="submit.php">
<input name="barcode" type="text" onkeyup="check(this);">
<input type="submit" value="go!">
please help me and if you can remove any unnecessary returns in my script, I need it to be as compact as possible (if you don't mind)

Thanks in adv.,
Chris Edmiston
edmiston00 is offline   Reply With Quote
Old 05-29-2009, 12:46 AM   PM User | #2
edmiston00
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
edmiston00 is an unknown quantity at this point
solved!

fixed it myself! way to go chris! i'm a noob but trial and error and well, some duh moments.

so basically if you want to validate a form for minimum length, then wait a few seconds for more input before it submits automatically here you go:
Code:
<script language="javascript">
var t; 
function checkid(field) {
clearTimeout(t); 
if ( field.value.length >= 9 ) { 
t = setTimeout("document.forms.myform.submit();",2000); 
} 
}
and the html... works with our without a submit button
Code:
<form method="post" name="myform" action="http://localhost/test3.html">
<input name="memid" type="text" onkeyup="checkid(this);">
<input type="submit" value="go">
edmiston00 is offline   Reply With Quote
Old 05-29-2009, 12:46 AM   PM User | #3
edmiston00
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
edmiston00 is an unknown quantity at this point
now just if i knew how to clean my code up to minimize the characters and lines it takes up.
edmiston00 is offline   Reply With Quote
Old 05-29-2009, 03:11 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,552
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Only thing I see to make it smaller is you don't need ".forms" in there.
Code:
t = setTimeout("document.myform.submit()",2000);
What the heck, you could name the form "f" instead. On, and language=... is obsolete and deprecated.
Code:
<script type="text/javascript">
var t; 
function c(f){ clearTimeout(t); 
if ( f.value.length > 8 ) t = setTimeout("document.f.submit()",2000); }
</script>
...
<form method="post" name="f" action="test3.html">
<input name="memid" type="text" onkeyup="c(this);">
</form>
You could get rid of line breaks and some of the spaces, but I don't see it getting any shorter than that.

No, wait. I lied.
Code:
<script type="text/javascript">
var t; 
</script>
...
<form method="post" name="f" action="test3.html">
<input name="memid"
  onkeyup="clearTimeout(t);if(this.value.length>8)t=setTimeout('document.f.submit()',2000)">
</form>
Notice that I got rid of type=text. That's the default, so not needed. And used >8 instead of >=9.

How's that?

Now the big question: Why does the size matter that much?
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
edmiston00 (05-29-2009)
Old 05-29-2009, 05:01 AM   PM User | #5
edmiston00
New to the CF scene

 
Join Date: May 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
edmiston00 is an unknown quantity at this point
Optimized

Thanks for taking a look and cleaning it up!

I'm not sure if size matters THAT much, but I do appreciate learning new and more optimized ways of writing code more than anything else.

I also don't know a whole lot about server loads and people trying to access the same file at the same instant.

But, if I have my way when I'm done I would have something that could be used to count over 200 SKUs/UPCs that may be in 2 locations 52 times a year in 1500 stores.

By my calculations you saved me 77 characters so:

77x1500x52x2x200 = 2,402,400,000 bytes! Two gigs of bandwidth (or about $1.00! If I follow the same approach for the whole program, I might be able to save 10 or 20 gigs a year!

In reality, I guess I'm just a minimalist, no good excuse.

Last edited by edmiston00; 05-29-2009 at 05:03 AM.. Reason: to say thanks
edmiston00 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:45 AM.


Advertisement
Log in to turn off these ads.