Advertisement

05.11.2008 at 12:35PM PDT, ID: 23393004
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.5

Need Help With Form Submission JavaScript

Asked by macrolific in JavaScript, Extensible HTML (XHTML), WebApplications

Tags:

My JavaScript verifies form fields and computes the total charge amount.  If the amount manually typed is incorrect, it corrects the amount, allows the user to acknowledge the corrected amount by hitting OK, and submits the form.  The user is given no opportunity to stop the form submission in the event that they don't like the new charge amount.

Here's the part script that corrects the amount (it's at the very end of the script):

      if (("$" + totprem) != document.enrollForm.cc_Amount.value) {
            alert("Premium is incorrect. Correct premium is $" + totprem);
            document.enrollForm.cc_Amount.value = "$" + totprem;
      }


Below is the whole script.  How can I change this so that, after the new charge amount is presented, the form is not automatically submitted?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
function getTotal() {
	var prem = 0;
	var spouse = 0;
	var sPrem = 0;
	var child = 0;
	var chRate = 0;
	var chPrem = 0;
	var totalPrem = 0;
	var repat_test = window.document.enrollForm.repatMed.checked;
	var period = document.enrollForm.coverageTerm.selectedIndex;
	
	if (period == 0) {
		alert("Please select coverage period");
		document.enrollForm.coverageTerm.focus();
		return false;
	}
	else if (period == 1) {
		prem = 1121;
		chRate = 1693;
	}
	else if (period == 2) {
		prem = 427;
		chRate = 645;
	}
	else if (period == 3) {
		prem = 716;
		chRate = 1082;
	}
	else {
		prem = 327;
		chRate = 493;
	}
	if (document.enrollForm.spouse_DOB.value != "" && document.enrollForm.spouse_Name.value == "") {
		alert("Please enter your spouse's name");
		document.enrollForm.spouse_Name.focus();
		return false;
	}
	if (document.enrollForm.child1_DOB.value != "" && document.enrollForm.child1_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child1_Name.focus();
		return false;
	}
	if (document.enrollForm.child2_DOB.value != "" && document.enrollForm.child2_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child2_Name.focus();
		return false;
	}
	if (document.enrollForm.child3_DOB.value != "" && document.enrollForm.child3_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child3_Name.focus();
		return false;
	}
	if (document.enrollForm.spouse_Name.value != "" && document.enrollForm.spouse_DOB.value == "") {
		alert("Please enter your spouse's date of birth");
		document.enrollForm.spouse_DOB.focus();
		return false;
	}
	if (document.enrollForm.child1_Name.value != "" && document.enrollForm.child1_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child1_DOB.focus();
		return false;
	}
	if (document.enrollForm.child2_Name.value != "" && document.enrollForm.child2_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child2_DOB.focus();
		return false;
	}
	if (document.enrollForm.child3_Name.value != "" && document.enrollForm.child3_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child3_DOB.focus();
		return false;
	}
	if (document.enrollForm.spouse_Name.value && document.enrollForm.spouse_DOB.value) {
		spouse = 1;
		if (period == 1) {sPrem = 4169;}
		else if (period == 2) {sPrem = 1589;}
		else if (period == 3) {sPrem = 2664;}
		else {sPrem = 1215;}
	}
	if (document.enrollForm.child1_Name.value && document.enrollForm.child1_DOB.value) {
		child = child + 1;
	}
	if (document.enrollForm.child2_Name.value && document.enrollForm.child2_DOB.value) {
		child = child + 1;
	}
	if (document.enrollForm.child3_Name.value && document.enrollForm.child3_DOB.value) {
		child = child + 1;
	}
	
	chPrem = chRate * child;
	
	if (child > 1) {
		if (period == 1) {chPrem = 3384;}
		else if (period == 2) {chPrem = 1290;}
		else if (period == 3) {chPrem = 2162;}
		else {chPrem = 986;}
	}
	if (repat_test == true) {
	   var numIns = 1 + spouse + child;
	   totalPrem = numIns * 40;
	}
	else {
	   totalPrem = prem + sPrem + chPrem;	   
	}
	return totalPrem;
}
 
function writePremium() {
	var totPrem = getTotal();
	if (totPrem == false) {return false;}
	else {document.enrollForm.cc_Amount.value = "$" + totPrem;}
 
}
 
function validateForm() {
 
	/*** check for blank fields ***/
	if (document.enrollForm.student_firstName.value == "") {
		alert("Please enter your first name");
		document.enrollForm.student_firstName.focus();
		return false;
	}
	if (document.enrollForm.student_lastName.value == "") {
		alert("Please enter your last name");
		document.enrollForm.student_lastName.focus();
		return false;
	}
	if (document.enrollForm.student_ID.value == "") {
		alert("Please enter your student ID");
		document.enrollForm.student_ID.focus();
		return false;
	}
	if (document.enrollForm.student_DOB.value == "") {
		alert("Please enter your date of birth");
		document.enrollForm.student_DOB.focus();
		return false;
	}
	if (document.enrollForm.student_Address.value == "") {
		alert("Please enter your street address");
		document.enrollForm.student_Address.focus();
		return false;
	}
	if (document.enrollForm.student_City.value == "") {
		alert("Please enter your city");
		document.enrollForm.student_City.focus();
		return false;
	}
	if (document.enrollForm.student_State.value == "") {
		alert("Please enter your state");
		document.enrollForm.student_State.focus();
		return false;
	}
	if (document.enrollForm.student_Zip.value == "") {
		alert("Please enter your zip code");
		document.enrollForm.student_Zip.focus();
		return false;
	}
	if (document.enrollForm.student_Phone.value == "") {
		alert("Please enter your phone number");
		document.enrollForm.student_Phone.focus();
		return false;
	}
	if (document.enrollForm.student_Phone.value.length < 12) {
		alert("Please enter a valid phone number");
		document.enrollForm.student_Phone.focus();
		return false;
	}
	if ((document.enrollForm.student_Phone.value.indexOf("-") == -1) || (document.enrollForm.student_Phone.value.indexOf("(") != -1) || (document.enrollForm.student_Phone.value.indexOf(")") != -1)) {
		alert("Please enter phone number in 123-123-1234 format");
		document.enrollForm.student_Phone.focus();
		return false;
	}
	if (document.enrollForm.student_Email.value == "") {
		alert("Please enter your e-mail address");
		document.enrollForm.student_Email.focus();
		return false;
	}
  	if ((document.enrollForm.student_Email.value.indexOf("://") != -1) || (document.enrollForm.student_Email.value.indexOf("@") == -1) || (document.enrollForm.student_Email.value.indexOf(".") == -1 )|| (document.enrollForm.student_Email.value.indexOf(" ") != -1 )|| (document.enrollForm.student_Email.value.length < 6)) {
  		alert("E-mail is invalid. Please enter valid e-mail address.");
  		document.enrollForm.student_Email.focus();
  		return false;
	}
	if (document.enrollForm.coverageTerm.selectedIndex == 0) {
		alert("Please choose coverage period");
		document.enrollForm.coverageTerm.focus();
		return false;
	}
	if (document.enrollForm.campusName.value == "") {
		alert("Please enter campus name");
		document.enrollForm.campusName.focus();
		return false;
	}
	if (document.enrollForm.spouse_DOB.value != "" && document.enrollForm.spouse_Name.value == "") {
		alert("Please enter your spouse's name");
		document.enrollForm.spouse_Name.focus();
		return false;
	}
	if (document.enrollForm.child1_DOB.value != "" && document.enrollForm.child1_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child1_Name.focus();
		return false;
	}
	if (document.enrollForm.child2_DOB.value != "" && document.enrollForm.child2_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child2_Name.focus();
		return false;
	}
	if (document.enrollForm.child3_DOB.value != "" && document.enrollForm.child3_Name.value == "") {
		alert("Please enter your child's name");
		document.enrollForm.child3_Name.focus();
		return false;
	}
	if (document.enrollForm.spouse_Name.value != "" && document.enrollForm.spouse_DOB.value == "") {
		alert("Please enter your spouse's date of birth");
		document.enrollForm.spouse_DOB.focus();
		return false;
	}
	if (document.enrollForm.child1_Name.value != "" && document.enrollForm.child1_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child1_DOB.focus();
		return false;
	}
	if (document.enrollForm.child2_Name.value != "" && document.enrollForm.child2_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child2_DOB.focus();
		return false;
	}
	if (document.enrollForm.child3_Name.value != "" && document.enrollForm.child3_DOB.value == "") {
		alert("Please enter your child's date of birth");
		document.enrollForm.child3_DOB.focus();
		return false;
	}
	if (document.enrollForm.cc_Number.value == "") {
		alert("Please enter the credit card number");
		document.enrollForm.cc_Number.focus();
		return false;
	}
	if (document.enrollForm.cc_Number.value.length < 11) {
		alert("Please enter a valid credit card number");
		document.enrollForm.cc_Number.focus();
		return false;
	}
	if (document.enrollForm.cc_firstName.value == "") {
		alert("Please enter the first name on the credit card");
		document.enrollForm.cc_firstName.focus();
		return false;
	}
	if (document.enrollForm.cc_lastName.value == "") {
		alert("Please enter the last name on the credit card");
		document.enrollForm.cc_lastName.focus();
		return false;
	}
	if (document.enrollForm.cc_Address.value == "") {
		alert("Please enter the credit card holder's street address");
		document.enrollForm.cc_Address.focus();
		return false;
	}
	if (document.enrollForm.cc_State.value == "") {
		alert("Please enter the credit card holder's state");
		document.enrollForm.cc_State.focus();
		return false;
	}
	if (document.enrollForm.cc_Zip.value == "") {
		alert("Please enter the credit card holder's zip code");
		document.enrollForm.cc_Zip.focus();
		return false;
	}
	if (document.enrollForm.cc_Number.value == "") {
		alert("Please enter the credit card number");
		document.enrollForm.cc_Number.focus();
		return false;
	}
	if (document.enrollForm.cc_cardCode.value == "") {
		alert("Please enter the card security code");
		document.enrollForm.cc_cardCode.focus();
		return false;
	}
	if (document.enrollForm.cc_expMo.value == "") {
		alert("Please enter credit card expiration month");
		document.enrollForm.cc_expMo.focus();
		return false;
	}
	if (document.enrollForm.cc_expYr.value == "") {
		alert("Please enter credit card expiration year");
		document.enrollForm.cc_expYr.focus();
		return false;
	}
	if (document.enrollForm.cc_Amount.value == "") {
		alert("Please click the Compute Premium button");
		document.enrollForm.cc_Amount.focus();
		return false;
	}
	
	var totprem = getTotal();
	
	if (("$" + totprem) != document.enrollForm.cc_Amount.value) {
		alert("Premium is incorrect. Correct premium is $" + totprem);
		document.enrollForm.cc_Amount.value = "$" + totprem;
	}
}
[+][-]05.11.2008 at 01:33PM PDT, ID: 21543405

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Extensible HTML (XHTML), WebApplications
Tags: JavaScript
Sign Up Now!
Solution Provided By: sinoj
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.11.2008 at 01:41PM PDT, ID: 21543425

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_2_20070628