Solved

javascript autotab and error checking

Posted on 2014-07-27
11
223 Views
Last Modified: 2014-07-27
i have a form with some required fields.  If the field is blank the pages pops up a warning.  It works fine on "normal" fields.
if(document.send_request.city.value==''){
		alert('Please enter city');
		document.send_request.city.focus();
		return false;

Open in new window


On some fields I have an autotab function.
<script language="javascript" type="text/javascript">
			  for(var l=0; l<=2; l++){
			  	var l1=l+1;
				  document.write('<input name="cvv_'+l+'" type="text" class="middletextfield" id="cvv_'+l+'" style="width:10px;" maxlength="1" onKeyup="autotab(this, document.send_request.cvv_'+l1+')"/>');
			  }/*send_request*/
			  </script>

Open in new window


my problem is the error checking doesnt work for fields that have the autotab.

What am I doing wrong?  

What does this line actually do
var l1=l+1;

Open in new window

0
Comment
Question by:livegirllove
  • 5
  • 4
11 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40222378
One thing it does is produce a visually confusing line of code!  I wish people would understand that so many errors and annoyances and timewasters can be avoided if you just omit the visually ambiguous letters and numbers from your variable names*.  The list is pretty simple: O (oh), 0 (zero) I (eye), 1 (one), l (ell).  In some fonts, 5 (five), S (ess).  In some fonts, 2 (two), Z (zee).

Taken by itself, the line of code uses the current value of l (ell) and adds one to it.  Then it assigns the resulting value to l1 (ell, one).  All by itself this is not too dramatic, but if you look at the line before it, you will see that it is a for() loop iterator that initially sets l (ell) to zero.  So this l1 (ell, one) variable is a pointer that looks ahead of the current value of the iterator.

* Omit these letters from your passwords, too.  Especially the auto-generated passwords.  And omit them from CAPTCHA images.  They're more trouble than they're worth!
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40222381
You need to set name attribute in the form, take a look as following code and its autotab function. I put javascript code  for input tag  between form tag, and variable "ll+1" is for the variable in autotab() function  so that you keyup on first input tab and the next input tab
will be focus , so it is ll+1;  1 means next input tag
Try input something in input box 1 and keyup and then you  will see next input box will be focused that is function of autotab

You can use browser developer tools click options->developer tolls->console, or Element, you can see all final code and its error.

Duncan
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>AutoTab Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function autotab(current,to){
if (current.getAttribute &&  current.value.length==current.getAttribute("maxlength")) {
        to.focus() 
}
}
</script>
	<body style="width:1000px; height:1000px">
	<form name="send_request">
	<textarea>
	This is testing
	</textarea>
			<script type="text/javascript">
	  for(var l=0; l<=2; l++){
			  	var l1=l+1;
				  document.write('<input name="cvv_'+l+'" type="text" class="middletextfield" id="cvv_'+l+'" style="width:10px;" maxlength="1" onKeyup="autotab(this, document.send_request.cvv_'+l1+')"/>');
			  }/*send_request*/	
			  </script>
	</form>
	
  </body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:livegirllove
ID: 40222384
if(document.send_request.cvv.value==''){
		alert('Please enter cvv);
		document.send_request.cvv.focus();
		return false;

Open in new window


this doesn't work.
what variable is it seeing.  When I post the form the variable is $cvv but the javascript doesn't seem to be seeing that.
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40222385
try review my previous post and try  my example code , and you will know it,

 where is your autotab funnction?
0
 
LVL 1

Author Comment

by:livegirllove
ID: 40222393
I attached the file.  The autotabs are inline in the form.

The error check is in the header and does the check on submit and pops up a warning message.  I don't want to rewrite this ancient terrible code I just need to know what variable I should have document.send_request.cvv.value?  

document.send_request.cvv+ll+.value
document.send_request.cvv_.value
document.send_request.cvv+i+.value


thanks
page08.html
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 13

Expert Comment

by:duncanb7
ID: 40222401
I run your page that is working, I type 1 at box 1on cvv, it will go and focus to next box, so on
the autotab function is okay no error.

Where is error area you are talking ?  I don't find any error and review your question post that
is not mentioned.

You need to write it in detail ,otherwise confusing

Duncan
0
 
LVL 1

Author Comment

by:livegirllove
ID: 40222410
the auto tab works.  If you enter nothing in the cvv box it should popup  a warning message.  It does not popup the message and allows the form to submit.

If you don't enter phone number the warning works correctly.
auto tab works.  error checking does not but only on fields that use autotab.  credit card and cvv
0
 
LVL 13

Accepted Solution

by:
duncanb7 earned 500 total points
ID: 40222424
Is it what you want for check() ? other check for input is similar
if(document.send_request.cvv_0.value==''){
		alert('Please enter cvv');
		document.send_request.city.focus();
		return false;
}
if(document.send_request.cvv_1.value==''){
		alert('Please enter cvv');
		document.send_request.city.focus();
		return false;
}
if(document.send_request.cvv_2.value==''){
		alert('Please enter cvv');
		document.send_request.city.focus();
		return false;
}

Open in new window

0
 
LVL 1

Author Comment

by:livegirllove
ID: 40222429
that works perfect thanks
0
 
LVL 1

Author Comment

by:livegirllove
ID: 40222476
you are correct its wrong.  but the point was I need cvv_0 etc.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now