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

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

Tpaul_10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
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
Tpaul_10Author Commented:
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
leakim971PluritechnicianCommented:
please post the page modified
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Tpaul_10Author Commented:
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
leakim971PluritechnicianCommented:
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
Tpaul_10Author Commented:

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
leakim971PluritechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tpaul_10Author Commented:
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
Tpaul_10Author Commented:
Let me try the last one.

Thanks
0
Tpaul_10Author Commented:
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
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.