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 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript If statement

    I am looking to validate a drop down list box (Motor_Frame), so when "Num" is selected from it, a value must be entered into the Motor_Frame_num text box.

    I am trying to use the following code but its not working:

    <script type="text/javascript">
    function validate_form (){
    Motor_Frame = document.getElementById('Motor_Frame');
    if(Motor_Frame.value.toString() != "Num")
    else if (Motor_Frame_num.value!="" )
    { alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
    document.forms[0].Motor_Frame_num.focus();
    return false;
    }
    return true;
    }


    </script>

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    946
    Thanks
    0
    Thanked 129 Times in 128 Posts
    You are missing a statement block before the else
    Even if you want nothing to happen if the conditionis true, it have to be there.

    change
    Code:
    if(Motor_Frame.value.toString() != "Num")
    to
    Code:
    if(Motor_Frame.value.toString() != "Num"){}
    And Motor_Frame.value is already a string
    so this line could be shortened to
    Code:
    if(Motor_Frame.value != "Num"){}
    You do also have a space between the function name and the parenthesis, that can cause troubles.

    try using this
    Code:
    function validate_form(){
    Motor_Frame = document.getElementById('Motor_Frame');
    if(Motor_Frame.value != "Num"){}
    else if (Motor_Frame_num.value!="" )
    { alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
    document.forms[0].Motor_Frame_num.focus();
    return false;
    }
    return true;
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Motor_Frame = document.getElementById('Motor_Frame');

    Do not use the same name for a Javascript variable and an HTML element.

    var MFrame = document.getElementById('Motor_Frame');

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have tried that code but i keep getting "query was empty" when saving my record to database. I am using php code....any ideas???

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    Code:
    <script type="text/javascript">
    function validate_form ()
    {
        var form = document.forms[0];
        if (    form.Motor_Frame.value == "Num" && form.Motor_Frame_num.value = "" )
        {
            alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
            form.Motor_Frame_num.focus();
            return false;
        }
        return true;
    }
    BUT... But that doesn't check to see of Motor_Frame_num is actually between 1 and 500!

    So:
    Code:
    <script type="text/javascript">
    function validate_form ()
    {
        var form = document.forms[0];
        if ( form.Motor_Frame.value != "Num" ) return true;
        var MFn = parseInt(form.Motor_Frame_num.value);
        if ( isNaN(MFn) || MFn < 1 || MFn > 500 )
        {
            alert("Please fill in Motor Frame Number with the correct value, a value between 1 and 500")
            form.Motor_Frame_num.focus();
            return false;
        }
        return true;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Sep 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    that code worked an the error appeared when i didnt enter anything into the motor_frame_num field but when i clicked save i still get Query was empty.....????

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,965
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    So now you have a PHP problem. Likely nothing to do with JavaScript.

    Time to learn how to debug PHP. And you probably need to post in the PHP forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by kerrymorris View Post
    that code worked an the error appeared when i didnt enter anything into the motor_frame_num field but when i clicked save i still get Query was empty.....????
    All user inputs must be validated in your server side script before using it further down.

    Client side validation (javascript) is a "nice to have" but not a "must have" since it can easily be turned off or bypassed alltogether by the user.

    Near the top of your php script, first make sure all sent data has been corectly received and contains only valid data. Only after all the expected data has been received and validated do you proceed with your php script.

    If any expected data is not received or invalid, display an appropriate error message, redirect back to the <form> or whatever and then terminate the php script.


  •  

    Posting Permissions

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