dcsimg
www.webdeveloper.com
Results 1 to 13 of 13

Thread: I'm stuck guys please help!

  1. #1
    Join Date
    Mar 2003
    Posts
    46

    I'm stuck guys please help!

    Hi,

    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
    }

    // End -->

    </script>


    <CENTER>

    <FORM name="formx">
    <input type="number" size=4 value="" name="a">
    <input type="number" size=4 value="" name="b">
    <input type="number" size=4 value="" name="c">
    <input type="number" size=4 value="" name="d">
    <input type="number" size=4 value="" name="e">
    <input type="number" size=4 value="" name="f">
    <input type="number" size=4 value="" name="g">
    <input type="number" size=4 value="" name="h"> <br>
    total
    <input type "number" value="" name="ans" size=9 onFocus=a_plus_h(form)>
    </FORM>
    </CENTER>

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    [font=arial]This line: else if (cert.value < 75){
    should look like this: else if (cert.value > 75){

    But returning false does not stop the form from processing, by the way.

  3. #3
    Join Date
    Mar 2003
    Posts
    46

    Yea, you're right getting NaN

    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 08:29 PM.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    I've gotta go, but here's a little bit of help:

    <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
    }

    // End -->

    </script>


    <CENTER>

    <FORM name="formx">
    <input type="number" size=4 value="" name="a">
    <input type="number" size=4 value="" name="b">
    <input type="number" size=4 value="" name="c">
    <input type="number" size=4 value="" name="d">
    <input type="number" size=4 value="" name="e">
    <input type="number" size=4 value="" name="f">
    <input type="number" size=4 value="" name="g">
    <input type="number" size=4 value="" name="h"> <br>
    total
    <input type "number" value="" name="ans" size=9 onFocus=a_plus_h(form)>
    </FORM>
    </CENTER>

    I'll answer your question(s) later or possibly tomorrow. Byebye!

  5. #5
    Join Date
    Mar 2003
    Posts
    46
    Thanks Jona,

    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

  6. #6
    Join Date
    Jan 2003
    Location
    England
    Posts
    52
    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
    }
    }

    // End -->

    </script>


    <CENTER>

    <FORM name="formx">
    <input type="number" size=4 value="" name="a">
    <input type="number" size=4 value="" name="b">
    <input type="number" size=4 value="" name="c">
    <input type="number" size=4 value="" name="d">
    <input type="number" size=4 value="" name="e">
    <input type="number" size=4 value="" name="f">
    <input type="number" size=4 value="" name="g">
    <input type="number" size=4 value="" name="h"> <br>
    total
    <input type "number" value="" name="ans" size=9 onFocus=a_plus_h()>
    </FORM>
    </CENTER>

    HTH

  7. #7
    Join Date
    Jan 2003
    Location
    England
    Posts
    52
    Oops, are posts just crossed!
    If you mean to put 75 in the Total box then after your alert put formx.ans.value=75

  8. #8
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Eh, you beat me to it... oh well.

    Jenn, if you have any other questions don't hesitate to ask.

  9. #9
    Join Date
    Mar 2003
    Posts
    46
    Thanks guys,

    You guys are so helpful and thanks for making this newbie experience a good one.

  10. #10
    Join Date
    Mar 2003
    Posts
    46

    actually I do have a question

    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 12:33 PM.

  11. #11
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413

    Re: actually I do have a question

    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?

  12. #12
    Join Date
    Mar 2003
    Posts
    46
    Hi Jona,

    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
    }
    }

    // End -->

    </script>


    <CENTER>

    <FORM name="formx">
    <input type="number" size=4 value="0" name="a">
    <input type="number" size=4 value="0" name="b">
    <input type="number" size=4 value="0" name="c">
    <input type="number" size=4 value="0" name="d">
    <input type="number" size=4 value="0" name="e">
    <input type="number" size=4 value="0" name="f">
    <input type="number" size=4 value="0" name="g">
    <input type="number" size=4 value="0" name="h"> <br>
    total
    <input type "number" value="0" name="i" size=9 onFocus=a_plus_h()>
    </FORM>
    </CENTER>

    When I tried in netscape when the total number goes over 75 the alert keeps poping up over and over, which crashes my brwoser almost. What can I do?

    Thanks.

    Jenn

  13. #13
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles