I am new to programming, hope this makes sense. I am writing a JavaScript on that will basically add numbers an user inputs in a form field. right now there are 8 fields. The script seems to work find until I decided I want to add a validation to the script. All I want is the validation to do is if the total goes over 75 and if the "total" input field is selected first a pop up alert will appear. Can't get it right ugh! frustrated and tired with no one to turn but this forum...below is my messy code...hope someone out there can fix this
Thanks.
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function a_plus_h(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=eval(form.c.value)
d=eval(form.d.value)
e=eval(form.e.value)
f=eval(form.f.value)
g=eval(form.g.value)
h=eval(form.h.value)
i=a+b+c+d+e+f+g+h
form.ans.value = i
var cert=document.formx.ans
if ((cert.value=="0")||(cert.value==null)){
alert("you have to fill in other fields first")
cert.focus()
return false
}
else if (cert.value < 75){
alert("hi you can only have max of 75")
cert.focus()
return false
}
return true
}
Yea, you're right. I think right now I just want the validation script to work. The form processing can come later. I'm still getting a nice piece of Indian NaN (not a number) when I click on the total box...ugh! What is wrong!
Thanks Jona for pointing that math syntax out for me.
Last edited by jennAshton; 03-13-2003 at 07:29 PM.
Why take life so seriously when you never come out of it alive.
<SCRIPT LANGUAGE="JavaScript">
function a_plus_h(form) {
a=eval(form.a.value)
b=eval(form.b.value)
c=eval(form.c.value)
d=eval(form.d.value)
e=eval(form.e.value)
f=eval(form.f.value)
g=eval(form.g.value)
h=eval(form.h.value)
i=a+b+c+d+e+f+g+h
form.ans.value = i
var cert=document.formx.ans
if ((cert.value=="0")||(cert.value==null)){
alert("You have to fill in other fields first")
cert.focus()
return false
}
else if (cert.value > 75){
alert("hi you can only have max of 75")
cert.value=""
cert.focus()
return false
}
return true
}
I found out why I keep getting NaN. Because the value in the form fields was not defind. I had value="" instead of value="0". Now I would like to find out if I the number is more than 75 it will change the last field to 75. For example, if the total is 80 and when somone clicks on the total field it will have the pop up and then writes 75 in that box...hopefully, it's clear.
Thanks for all your help!
Jenn
Why take life so seriously when you never come out of it alive.
Hope you don't mind me butting in but this is my suggestion...
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function a_plus_h() {
a=eval(formx.a.value)
b=eval(formx.b.value)
c=eval(formx.c.value)
d=eval(formx.d.value)
e=eval(formx.e.value)
f=eval(formx.f.value)
g=eval(formx.g.value)
h=eval(formx.h.value)
i=a+b+c+d+e+f+g+h
if ((i=="0")||(isNaN(i))){
alert("you have to fill in other fields first")
formx.a.focus()
return false
}
else {
if (i > 75){
alert("hi you can only have max of 75")
formx.a.focus()
return false
}
formx.ans.value = i
formx.ans.focus()
return true
}
}
I was testing out the script and noticed that the alert pops up if the fields are left blank. I would like to know if that could be bypassed. It's okay for the users to leave some of the boxes blank.
Here is the logic I guess:
User fill in numbers in any field box (not all form fields need a number)
If the number A thru I is more than 75 then an alert goes off.
The alert triggers a script and rewrites the total form field to 75 if the number in total form field is more than 75.
Is it confusing? Meanwhile, I'll play around with the script.
Thaks for helping.
Oops, I didn't see your comment Cyber. I think I may have it!
Thanks!!!! You guys are SUPER!
Cheers!
Last edited by jennAshton; 03-14-2003 at 11:33 AM.
Why take life so seriously when you never come out of it alive.
Originally posted by jennAshton I was testing out the script and noticed that the alert pops up if the fields are left blank. I would like to know if that could be bypassed. It's okay for the users to leave some of the boxes blank.
You want the alert to only go off in NONE of the fields are filled in? But if at least one of them is filled in, it goes off?
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
I have the script all figured out. However I'm having a little issue in netscape 4.7x
below is my new script:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function a_plus_h() {
a=eval(document.formx.a.value)
b=eval(document.formx.b.value)
c=eval(document.formx.c.value)
d=eval(document.formx.d.value)
e=eval(document.formx.e.value)
f=eval(document.formx.f.value)
g=eval(document.formx.g.value)
h=eval(document.formx.h.value)
i=a+b+c+d+e+f+g+h
if ((i=="0")){
alert("you have to fill in other fields first")
document.formx.a.focus()
return false
}
else {
if (i > 75){
alert("hi you can only have max of 75")
document.formx.i.value=75
document.formx.i.focus()
return false
}
document.formx.i.value = i
document.formx.i.focus()
return true
}
}
You can try making it return false after the alert then focus on 'i' instead of it alerting, focusing on i, and then returning false. I don't often script for NN4.x. You can also try changing this: if (i > 75){ to this: if (i => 75){. That would make the logic: if "i" is equal to or greater than 75.
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
Bookmarks