Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    zil
    zil is offline
    New Coder
    Join Date
    Jul 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Error in validating null fields...

    Hello everyone!
    Im trying a simple validation...i have 3 fields and if ever one of the 3 fields is not empty, you need to fill all the 3 fields...in fact its about an application form where a person needs to fill in his Bachelor Degree Title, Grade and University...if ever the person enter his/her Bachelor Title, the person needs to enter a grade and the University attended and so on...well my codes are as follows:

    <script language="javascript">
    function checkDegree(form)
    {
    var dTitle, dGrade, dUniv

    dTitle = form.B_Degree_Title;
    dGrade = form.B_Degree_Grade;
    dUniv = form.B_Degree_Univ;

    if (dTitle.value != null && dGrade.selectedIndex == 0)
    {
    alert ("Select a grade for your Bachelor's Degree please.");
    return (false);
    }
    else if (dTitle.value != null && dUniv.value == null)
    {
    alert ("Fill in a a value for the University field for your Bachelor's Degree please.");
    return (false);
    }
    else if (dGrade.selectedIndex > 0 && dTitle.value == null)
    {
    alert ("Fill in a a value for the Title field for your Bachelor's Degree please.");
    return (false);
    }
    else if (dGrade.selectedIndex > 0 && dUniv.value == null)
    {
    alert ("Fill in a a value for the University field for your Bachelor's Degree please.");
    return (false);
    }
    else if (dUniv.value != null && dTitle.value == null)
    {
    alert ("Fill in a a value for the Title field for your Bachelor's Degree please.");
    return (false);
    }
    else if (dUniv.value != null && dGrade.selectedIndex == 0)
    {
    alert ("Select a grade for your Bachelor's Degree please.");
    return (false);
    }
    }
    </script>

    Now the problem is that i have a link and as the person clicks on the link, the validation should run...but im having this error message:

    Error:'B_Degree_Title' is null or not an object

    The B_Degree_Title is the name of the title text box. Can anyone help me please i would be very grateful, thx.

    _______________
    Zil.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Is the case correct? How are you passing the form argument to the function? And you shouldn't check for null values like

    if (dTitle.value != null && ...

    as field values will never be null. You want to check for empty not null.

    if (dTitle.value != "" && ...
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by zil
    Error:'B_Degree_Title' is null or not an object

    The B_Degree_Title is the name of the title text box.
    not according to the document, it isn't. hence the error. your script can't find the element, hence it thinks it's not an object.

    check the spelling first.

    second, what it the value of "form" being passed to checkDegree?

  • #4
    zil
    zil is offline
    New Coder
    Join Date
    Jul 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes in fact ive also check for null fields as B_Degree_Title = "" and the same error occurs...the form paramater will normally be the name of my form, in that case degree (<form ACTION="" METHOD="POST" name="degree">)...im calling the function as follows:
    <a href="" onMouseDown="checkDegree(this.form)"> Save and Continue</a>

    btw, something seems very strange, if i call the function from an input text box through the Onclick event, the validation works. For e.g.
    <input type="text" name="Test" onClick="checkDegree(this.form)"></input>
    The above way the validation works but i can unfortunately not valid a form likewise.
    ____________
    Zil.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    <a href="" onMouseDown="checkDegree(this.form)"> Save and Continue</a>

    wount work, and it is not weird at all. this.form is a substitute for form object to be use only in relationship with the form's elements (input, textarea, select, etc). But <a> is a simple tag, not a form's element

    Use the full reference and you will get rid of problems

    function checkDegree()
    {
    var dTitle, dGrade, dUniv

    dTitle = document.forms['form_name'].B_Degree_Title;
    dGrade = document.forms['form_name'].B_Degree_Grade;
    dUniv = document.forms['form_name'].B_Degree_Univ;
    ...
    ...
    and

    <a href="#" onMouseDown="checkDegree()"> Save and Continue</a>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    zil
    zil is offline
    New Coder
    Join Date
    Jul 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you guyzzz!!:))

    Kor: Oh i c!!!im gonna try it now...hey guys ur really fantastic!!
    Thx a lot mates!!

    _______________
    Zil.

  • #7
    zil
    zil is offline
    New Coder
    Join Date
    Jul 2004
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    That's wonderful!

    Kor: Hey it really does work!!!!!
    I have been struggling on that for hours...thx a lot.
    Thx to others 2 coz uve been helping a lot 2, eventhough through small comments, that's largely great help for me

    _____________
    Zil.


  •  

    Posting Permissions

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