Solved

Using the Enter button or Submit button to submit the form.

Posted on 2011-03-17
11
262 Views
Last Modified: 2012-05-11
Experts,

I am using the following javascript function to allow the users submit the form by clicking the
"Enter" button or by clicking the submit button on the form.

The code works fine when the user hits the enter button, but doesn't work when the user click the submit
button on the form.
Error : "e" is undefined.

Can you please correct me what I am doing wrong?

<!-- My Java Script function-->
<script language="JavaScript">
      document.onkeypress = processKey;
      
      function processKey(e)
      {
      
        if (event.keyCode == 13)  {
            validateForm() ;      
        }
      
      }

      function validateForm(){
      //my form validations and upon succesful validation, submit the form.
      
      myForm.submit();      
      }
</script>

<!-- My Submit button.-->

<a href="JavaScript:processKey(e);"><img src="submit.gif" alt="Submit" border="0" align="center"></a>

Thanks
<!-- My Java Script function-->
<script language="JavaScript">
	document.onkeypress = processKey;
	
	function processKey(e)
	{
	
	  if (event.keyCode == 13)  {
		validateForm() ;	
	  }
	
	}

	function validateForm(){ 
	//my form validations and upon succesful validation, submit the form.
	
	myForm.submit();	
	}
</script>

<!-- My Submit button.-->

<a href="JavaScript:processKey(e);"><img src="submit.gif" alt="Submit" border="0" align="center"></a>

Open in new window

0
Comment
Question by:Tpaul_10
  • 6
  • 5
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35159396
Try this :

(but as you know click is not a event from the keyboard)
function processKey -> function processKeyAndMouse()

<html>
<head>
<script language="JavaScript">
	document.onkeypress = processKey;
	
	function processKey(event)
	{
		if( event.type == "click" ) {
			validateForm();
		}
		var key = (window.event)?(window.event.keyCode):(event.which); // Mozilla vs MS
		if (key == 13)  {
			validateForm() ;	
		}	
		return false;
	}

	function validateForm(){ 
		//my form validations and upon succesful validation, submit the form.
		var myForm = document.getElementsByTagName("form")[0];
		myForm.submit();	
	}
</script>
</head>
<body>
<form action="http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26894391.html" >
<a href="javascript:void(0)" onclick="return processKey(event);"><img src="http://onthedefense.files.wordpress.com/2010/03/nuclear_button_440.jpg" alt="Submit" border="0" align="center"></a>
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Tpaul_10
ID: 35159866
Thanks for the quick reply. Here are the problems I am seeing now.

1) If I enter the values in form fileds and hit enter, gives an error
2) if I don't enter the values and hit enter  gives an error
3) As soon as I start typing the values in the form fields gives an error

Same error for all the above 3 scenarios.
Type is Null or not an object.
Code 0

Is this related to if(event.type == "click" )?

Thanks
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35159934
please post the page modified
0
 

Author Comment

by:Tpaul_10
ID: 35160135
Here is the code

Thanks
<html>
<head>
<script language="JavaScript">
	document.onkeypress = processKey

function processKey(event)
	{
		if(event.type == "click" ) {
			validateForm();
		}
		var key = (window.event)?(window.event.keyCode):(event.which); // Mozilla vs MS
		if (key == 13)  {
			validateForm() ;	
		}	
		return false;
	}

function validateForm(){ 

if (document.myForm.text1.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 1 ");
			document.myForm.text1.focus();
			return; 
		} 

if (document.myForm.text2.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 2");
			document.myForm.text2.focus();
			return; 
		} 


	if (error != 1)
		{
		myForm.submit();	
			return true; 
		}
	}
	
</script>
</head>
<body>
<form name="myForm" action="index.cfm" method="post">
<a href="javascript:void(0)" onclick="return processKey(event);"><img src="submit.gif" alt="submit" border="0" align="middle"></a>
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35160178
first error for this line : document.myForm.text1.value == ""
second error : document.myForm.text2.value == ""

the following work fine for me on IE, FF and Chrome  :
<html>
<head>
<script language="JavaScript">
	document.onkeypress = processKey

function processKey(event)
	{
		if(event.type == "click" ) {
			validateForm();
		}
		var key = (window.event)?(window.event.keyCode):(event.which); // Mozilla vs MS
		if (key == 13)  {
			validateForm() ;	
		}	
		return false;
	}

function validateForm(){ 

if (document.myForm.text1.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 1 ");
			document.myForm.text1.focus();
			return; 
		} 

if (document.myForm.text2.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 2");
			document.myForm.text2.focus();
			return; 
		} 


	if (error != 1)
		{
		myForm.submit();	
			return true; 
		}
	}
	
</script>
</head>
<body>
<form name="myForm" action="http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26894391.html" method="post">
<a href="javascript:void(0)" onclick="return processKey(event);"><img src="http://onthedefense.files.wordpress.com/2010/03/nuclear_button_440.jpg" alt="submit" border="0" align="middle"></a>
<input type="text" value="123" name="text1" />
<input type="text" value="123" name="text2" />
</form>
</body>
</html>

Open in new window

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

 

Author Comment

by:Tpaul_10
ID: 35160308

I didn't get about these two lines,
first error for this line : document.myForm.text1.value == ""
second error : document.myForm.text2.value == ""

As soon as I start typing the values in any of the text box, i am getting the error "type" error and I am using IE 8

Thanks
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35160670
Try this one :

<html>
<head>
<script language="JavaScript">
	document.onkeypress = processKey

function processKey(event)
	{
		if(window.event) { // Mozilla vs MS
			if(window.event.type == "click" ) {
				validateForm();
			}
			var key = window.event.keyCode;
			if (key == 13)  {
				validateForm() ;	
			}	
		}
		else {
			if(event.type == "click" ) {
				validateForm();
			}
			var key = event.which;
			if (key == 13)  {
				validateForm() ;	
			}	
		}
		return true;
	}

function validateForm(){ 

if (document.myForm.text1.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 1 ");
			document.myForm.text1.focus();
			return; 
		} 

if (document.myForm.text2.value == "") 
		{ 
			var		error = 1;
			alert("Please enter value 2");
			document.myForm.text2.focus();
			return; 
		} 


	if (error != 1)
		{
		myForm.submit();	
			return true; 
		}
	}
	
</script>
</head>
<body>
<form name="myForm" action="http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26894391.html" method="post">
<a href="javascript:void(0)" onClick="return processKey(event);"><img src="http://onthedefense.files.wordpress.com/2010/03/nuclear_button_440.jpg" alt="submit" border="0" align="middle"></a>
<input type="text" value="123" name="text1" />
<input type="text" value="123" name="text2" />
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Tpaul_10
ID: 35160684
leakim971:,

The code you have specified is not working. Please have value="" in the form fileds and  type in some values and see and it gives the error I am talking about.

Thanks for your help and appreciate any other ideas to fix this/
0
 

Author Comment

by:Tpaul_10
ID: 35160693
Let me try the last one.

Thanks
0
 

Author Comment

by:Tpaul_10
ID: 35161658
THANK YOU leakim971.
Out of all the questions I have asked so far this year, this is the best answer I have got fot what I was looking. THANKS Again for all your information and help.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 35161718
You're welcome! Thanks for the points!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now