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?

Improve company productivity with a Business Account.Sign Up

x
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
leakim971PluritechnicianCommented:
please post the page modified
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.