Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter key behavior on a form 1

Status
Not open for further replies.

awingnut

Programmer
Joined
Feb 24, 2003
Messages
759
Location
US
I have a form with an onClick event (javascript) for the submit button. When the button is clicked it works fine. However, when the enter key is pressed, the form is submitted but the javascript is not executed. Is this the way it is supposed to work? If so, what do I need to do to get the enter key to run the javascript as well? TIA.
 
You need to move the javascript action from the submit button to the form itself:
Code:
<form [COLOR=red]onSubmit="[COLOR=grey]// javascript code here[/color]"[/color] >

<marc>
 
Can I take back my star? :-)

I don't know what I did wrong but now the enter key does nothing and when I click the button the form gets submitted but the javascript is by-passed.
 
you might have to do

<form onsubmit="[red]return[/red] yourfunctionhere"

then change your button to a submit
 
FWIW, here is my form tag:
Code:
<form action="employmentapp.php" method="post" name="employmentapp" id="employmentapp" enctype="multipart/form-data" onSubmit="return checkForm(document.employmentapp);">
 
You want me to post my javascript? It just returns either true or false but it is quite large. However, it works fine when I used it as orignally stated so the script itself is debugged. The issue must be related to different behavior of onSubmit in the form tag vs. onClick in the button tag.
 
i do form validation (returning true/false)all the time with onsubmit, only time i get the symptom you are getting is when something is wrong in the javascript

what i do is run the script firefox then go under tools > javascript console to see if a error comes up
 
Did that. No luck. The javascript console is clear. However, I did find a logic error which explains why I thought the submit button was submitting the form but no running the script. Now I have the submit button working correctly but the enter key seems to be a no-op.
 
show your code.

and this is unnecessary:

Code:
onSubmit="return checkForm(document.employmentapp);"

you can just do this:

Code:
onsubmit="return checkForm(this);"

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Which code (HTML or javascript)? The javascript is huge.
 
Typically, the enter key is only truly cross-browser compatible for forms with a submit button, not just a button which calls submission code. Also, the code fragment document.employmentapp is IE specific; cLFlaVA's replacement is not and therefore highly preferable.
 
That is large too but here it is:
Code:
function Checkform(theForm) {
		var errmsg="";
		errmsg+=checkBox(theForm.learn,"How you learned about us")
		if (theForm.learn[6].checked) {
			errmsg+=isEmpty(theForm.other.value,"The 'Other' source through which you learned about us");
		}
		errmsg+=isEmpty(theForm.lastname.value,"Last name");
		errmsg+=isEmpty(theForm.firstname.value,"First name");
		errmsg+=isEmpty(theForm.street.value,"Street");
		errmsg+=isEmpty(theForm.city.value,"City");
		errmsg+=isEmpty(theForm.state.value,"State");
		errmsg+=isEmpty(theForm.zip.value,"Zip code");
		if (theForm.appemail.value.length>0) {
			errmsg+=checkEmail(theForm.appemail.value,"Your email address");
		}
		errmsg+=checkPhone(theForm.dayphone.value,"Your daytime phone number");
		if (theForm.homephone.value.length>0) {
			errmsg+=checkEmail(theForm.homephone.value,"Your home phone number");
		}
		if (theForm.cellphone.value.length>0) {
			errmsg+=checkEmail(theForm.cellphone.value,"Your cell phone number");
		}
		errmsg+=checkTime(theForm.calltime.value,"Best time to call");
		errmsg+=checkBox(theForm.ampm,"AM or PM for best call time");
		errmsg+=checkBox(theForm.prevapp,"Previous application answer");
		if (theForm.prevapp[0].checked) {
			errmsg+=checkDate(theForm.prevappdate.value,"Previous application date");
		}
		errmsg+=checkBox(theForm.prevemp,"Previous employment answer");
		if (theForm.prevemp[0].checked) {
			errmsg+=checkDate(theForm.prevempdate.value,"Previous hire date");
		}
		if (theForm.relfr[0].checked) {
			errmsg+=checkDate(theForm.relfrname.value,"Name of relative or friend");
		}
		errmsg+=checkBox(theForm.currentemp,"Current employment status");
		if (theForm.currentemp[0].checked) {
			errmsg+=checkBox(theForm.contact,"Answer to contacting present employer");
		}
		errmsg+=checkBox(theForm.status,"Citizenship or immigration status");
		errmsg+=checkDate(theForm.availdate.value,"Availability date");
		errmsg+=checkDollars(theForm.salary.value,"Desired annual salary");
		errmsg+=checkBox(theForm.avail,"Times available to work");
		if (theForm.avail[1].checked) {
			errmsg+=checkBox(theForm.sess,"Part time work session");
		}
		errmsg+=isEmpty(theForm.highschool.value,"High school dimploma or GED");
		errmsg+=checkNumber(theForm.hsyear.value,"High school graduation or GED year");
		if (theForm.under.value.length>0) {
			errmsg+=isEmpty(theForm.unmajor.value,"Undergraduate major");
			errmsg+=checkNumber(theForm.unyear.value,"Undergraduate graduation year,last year attended or expected graduation year");
			errmsg+=isEmpty(theForm.undeg.value,"Undergraduate degree (use NC for Not Completed)");
		}
		if (theForm.grad.value.length>0) {
			errmsg+=isEmpty(theForm.grmajor.value,"Advanced degree major");
			errmsg+=checkNumber(theForm.gryear.value,"Advanced degree graduation year,last year attended or expected graduation year");
			errmsg+=isEmpty(theForm.grdeg.value,"Advanced degree (use NC for Not Completed)");
		}
		if (theForm.emp1.value.length>0) {
			errmsg+=isEmpty(theForm.emp1addr.value,"Employer 1 address (or former address)");
			if (theForm.emp1phone.value!="NA") {
				errmsg+=checkPhone(theForm.emp1phone.value,"Employer 1 phone number (use 'NA' if no longer in business)");
			}
			errmsg+=isEmpty(theForm.emp1title.value,"Job title for employer 1");
			errmsg+=isEmpty(theForm.emp1mgr.value,"Manager's name for employer 1");
			errmsg+=isEmpty(theForm.emp1reason.value,"Reason for leaving employer 1");
			errmsg+=checkBox(theForm.emp1contact,"Answer to contacting employer 1");
			errmsg+=checkDate(theForm.emp1stdate.value,"Employer 1 start date");
			errmsg+=checkDate(theForm.emp1nddate.value,"Employer 1 end date");
			errmsg+=checkDollars(theForm.emp1stsal.value,"Employer 1 starting salary");
			errmsg+=checkDollars(theForm.emp1ndsal.value,"Employer 1 final salary");
			errmsg+=isEmpty(theForm.emp1resp.value,"Responsibilities for employer 1");
		}
		if (theForm.emp2.value.length>0) {
			errmsg+=isEmpty(theForm.emp2addr.value,"Employer 2 address (or former address)");
			if (theForm.emp2phone.value!="NA") {
				errmsg+=checkPhone(theForm.emp2phone.value,"Employer 2 phone number (use 'NA' if no longer in business)");
			}
			errmsg+=isEmpty(theForm.emp2title.value,"Job title for employer 2");
			errmsg+=isEmpty(theForm.emp2mgr.value,"Manager's name for employer 2");
			errmsg+=isEmpty(theForm.emp2reason.value,"Reason for leaving employer 2");
			errmsg+=checkBox(theForm.emp2contact,"Answer to contacting employer 1");
			errmsg+=checkDate(theForm.emp2stdate.value,"Employer 2 start date");
			errmsg+=checkDate(theForm.emp2nddate.value,"Employer 2 end date");
			errmsg+=checkDollars(theForm.emp2stsal.value,"Employer 2 starting salary");
			errmsg+=checkDollars(theForm.emp2ndsal.value,"Employer 2 final salary");
			errmsg+=isEmpty(theForm.emp2resp.value,"Responsibilities for employer 2");
		}
		if (theForm.emp3.value.length>0) {
			errmsg+=isEmpty(theForm.emp3addr.value,"Employer 3 address (or former address)");
			if (theForm.emp3phone.value!="NA") {
				errmsg+=checkPhone(theForm.emp3phone.value,"Employer 3 phone number (use 'NA' if no longer in business)");
			}
			errmsg+=isEmpty(theForm.emp3title.value,"Job title for employer 3");
			errmsg+=isEmpty(theForm.emp3mgr.value,"Manager's name for employer 3");
			errmsg+=isEmpty(theForm.emp3reason.value,"Reason for leaving employer 3");
			errmsg+=checkBox(theForm.emp3contact,"Answer to contacting employer 3");
			errmsg+=checkDate(theForm.emp3stdate.value,"Employer 3 start date");
			errmsg+=checkDate(theForm.emp3nddate.value,"Employer 3 end date");
			errmsg+=checkDollars(theForm.emp3stsal.value,"Employer 3 starting salary");
			errmsg+=checkDollars(theForm.emp3ndsal.value,"Employer 3 final salary");
			errmsg+=isEmpty(theForm.emp3resp.value,"Responsibilities for employer 3");
		}
		if (theForm.emp4.value.length>0) {
			errmsg+=isEmpty(theForm.emp4addr.value,"Employer 4 address (or former address)");
			if (theForm.emp4phone.value!="NA") {
				errmsg+=checkPhone(theForm.emp4phone.value,"Employer 4 phone number (use 'NA' if no longer in business)");
			}
			errmsg+=isEmpty(theForm.emp4title.value,"Job title for employer 4");
			errmsg+=isEmpty(theForm.emp4mgr.value,"Manager's name for employer 4");
			errmsg+=isEmpty(theForm.emp4reason.value,"Reason for leaving employer 4");
			errmsg+=checkBox(theForm.emp4contact,"Answer to contacting employer 4");
			errmsg+=checkDate(theForm.emp4stdate.value,"Employer 4 start date");
			errmsg+=checkDate(theForm.emp4nddate.value,"Employer 4 end date");
			errmsg+=checkDollars(theForm.emp4stsal.value,"Employer 4 starting salary");
			errmsg+=checkDollars(theForm.emp4ndsal.value,"Employer 4 final salary");
			errmsg+=isEmpty(theForm.emp4resp.value,"Responsibilities for employer 4");
		}
		if (theForm.keying.checked) {
			if (theForm.kspeed.value.length==0) errmsg+="Keyboard words per minute is required.\n";
			else errmsg+=checkNumber(theForm.kspeed.value,"Keyboard words per minute");
		}
		if (isEmpty(theForm.ref1.value,"").length>0 || isEmpty(theForm.ref1.value,"").length>0 || isEmpty(theForm.ref3.value,"").length>0) {
			errmsg+="Three references are required.\n";
		}
		else {
			errmsg+=checkPhone(theForm.ref1phone.value,"Phone for reference 1");
			errmsg+=checkTime(theForm.ref1time.value,"Best time to call for reference 1");
			errmsg+=checkBox(theForm.ref1ampm,"AM or PM for best time to call reference 1");
			errmsg+=isEmpty(theForm.ref1occ.value,"Occupation or position for reference 1");
			errmsg+=checkPhone(theForm.ref2phone.value,"Phone for reference 2");
			errmsg+=checkTime(theForm.ref2time.value,"Best time to call for reference 2");
			errmsg+=checkBox(theForm.ref2ampm,"AM or PM for best time to call reference 2");
			errmsg+=isEmpty(theForm.ref2occ.value,"Occupation or position for reference 2");
			errmsg+=checkPhone(theForm.ref3phone.value,"Phone for reference 3");
			errmsg+=checkTime(theForm.ref3time.value,"Best time to call for reference 3");
			errmsg+=checkBox(theForm.ref3ampm,"AM or PM for best time to call reference 3");
			errmsg+=isEmpty(theForm.ref3occ.value,"Occupation or position for reference 3");
		}
		if (theForm.resume.value.length>0) {
			if (theForm.resume.value.substr(-4)!=".doc" && theForm.resume.value.substr(-4)!=".txt" && theForm.resume.value.substr(-4)!=".rtf") {
				errmsg+="The resume file must be a '.doc', '.rtf', or '.txt' file.\n";
			}
		}
		if (errmsg.length>0) {
			alert(errmsg);
			return false;
		}
		else {
			var confirmtext="By clicking 'OK' below, I agree to the following and certify that answers given herein are true and complete.\nI authorize investigation of all statements in this application for employment as may be necessary in arriving at an employment decision.\nThis application for employment shall be considered active for a period of time not to exceed 45 days. Any applicant wishing to be considered for employment beyond this time period should inquire as to whether or not applications are being accepted at that time.\n I hereby understand and acknowledge that, unless otherwise defined by applicable law, any employment relationship with this organization is of an 'at will' nature, which means that the Employee may resign at any time and the Employer may discharge the Employee at any time with or without cause. It is further understood that the 'at will' employment relationship may not be changed by a written document or by conduct unless such change is specifically acknowledged in writing by an authorized executive of this organization.\nIn the event of employment, I understand that false or misleading information given in my application or interview(s) may result in discharge. I understand, also, that I am required to abide by all rules and regulations of the employer.\nI understand that a criminal background investigation is required and the result will be a condition of employment."
			var cnfrm=confirm(confirmtext);
			if (cnfrm) return true;
			else return false;
		}
	}
 
i don't see why that wouldn't work. try alerting the error message either way to see what shows up as a debugging method.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
I did that. For whatever reason the enter key simply does not submit the form or execute the javascript.

Is there some other tag element (other then <form>, there is only 1 <form> tag on the page) where I could have inadvertently put something that could prevent the enter key from behaving normally?
 
checkForm, Checkform... ?

------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Try heading up your script function with the code
Code:
alert(theForm);
and make sure that it's talking about a form object. If it doesn't even run that as the first line'o'code for the submit function, then you'll also know it's not being called. I'll reiterate my earlier point - your button is <input type="submit" ...> ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top