Solved

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

Posted on 2011-03-17
11
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 69
jQuery scrollTop 2 40
Webpage error - Unable to get property "apply" of undefined or null reference 4 32
javascript date picker in php while loop 5 22
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…

911 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