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 to the CF scene
    Join Date
    Oct 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Compare dropdown value and textbox value

    Hi,
    I need my users to choose a value from a dropdown and thentype the same value in a text box. On submit i want to check if they are the same. How do I do that? please help.

    if(document.add_prod.prod_id.options[document.add_prod.prod_id.selectedIndex].value != document.add_prod.prod_id_chk.value )
    {
    alert("Please enter same Product ID.");
    document.add_prod.prod_id_chk.focus();

    }
    This is the code that I am trying to use but does not work.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    looks like it should work, do the options have a value attribute?
    I am the luckiest man in the world

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    ...or are you calling the function in the form onsubmit handler or onclick of a submit button?
    Code:
    function validate(f){
      if (f.prod_id.options[f.prod_id.selectedIndex].value != f.prod_id_chk.value )
     {
        alert("Please enter same Product ID.");
        f.prod_id_chk.focus();
        return false;
     } 
      //other validations here
      //...
      return true; //no error, continue submission
    }
    ...
    <form name="add_prod" action="..." method="post" onsubmit="return validate(this)">
    ...
    <input type="submit" value="Submit">
    or:
    Code:
    <form name="add_prod" action="..." method="post">
    ...
    <input type="submit" value="Submit" onclick="return validate(this.form)">
    </form>
    The 1st method is recommended, though...

  • #4
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Compare dropdown value and textbox value / To Roef and Glen

    roef,
    Yes the option has a value but comes from the database.

    <form action="insert_add_prod.asp" method="post" name="add_prod" id="add_prod" OnSubmit="return ValidateFields(this)">

    <select name="prod_id" id="prod_id" >
    <%
    'Execute your Query using MyConnection
    Do Until Myrs.eof
    %>
    <option value="<% = Myrs.Fields("item_id") %> ">
    <% = Myrs.Fields("item_id") %>
    </option>
    <%
    'Move to the next record
    Myrs.movenext
    loop
    %>
    </select>

    Is that a problem? This is the code I am using to validate;

    else if(document.add_prod.prod_id.options[document.add_prod.prod_id.selectedIndex].value != document.add_prod.prod_id_chk.value )
    {
    alert("Please enter same Product ID.");
    document.add_prod.prod_id_chk.focus();
    return false;

    }

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    answered in the other thread you started about this exact same subject
    (saw that one before this one)
    I am the luckiest man in the world

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Is that a snippet of the ValidateFields() function?
    You've now added the return false but it's still not working?
    Can you post the whole ValidateFields() function?

  • #7
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here is the full Validation Function

    <SCRIPT LANGUAGE="JavaScript">
    <!--start

    function ValidateFields()
    {
    if(NoBlanks())
    return true;

    return false;

    }


    function NoBlanks()
    {
    if(document.add_prod.prod_id_chk.value == "")
    {
    alert("Please enter Product ID.");
    document.add_prod.prod_id_chk.focus();
    }

    else if(document.add_prod.prod_id.options[document.add_prod.prod_id.selectedIndex].value != document.add_prod.prod_id_chk.value )
    {
    alert("Please enter same Product ID.");
    document.add_prod.prod_id_chk.focus();


    }

    else if(document.add_prod.add_manu_prod_id.value == "")
    {
    alert("Please enter Manufacturer ID.");
    document.add_prod.add_manu_prod_id.focus();
    }

    else
    return true;
    return false;

    }


    //end-->
    </SCRIPT>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You should put return false if blank entry is encountered and return true at the end.

    Code:
    function NoBlanks()
    {
        if(document.add_prod.prod_id_chk.value == "")
        {
            alert("Please enter Product ID.");
            document.add_prod.prod_id_chk.focus();
            return false;
        }
    
        else if(document.add_prod.prod_id.options[document.add_prod.prod_id.selectedIndex].value !=         document.add_prod.prod_id_chk.value )
        {
            alert("Please enter same Product ID.");
            document.add_prod.prod_id_chk.focus();
            return false;
        } 
        
        else if(document.add_prod.add_manu_prod_id.value == "")
        {
            alert("Please enter Manufacturer ID.");
            document.add_prod.add_manu_prod_id.focus();
            return false;
        }
        return true;
    }
    The code can be simplified and optimized like this:
    Code:
    function ValidateFields(f)
    {
        if(NoBlanks(f)) return true;
        return false;
    }
    
    function NoBlanks(f)
    {
        if(f.prod_id_chk.value == "")
        {
            alert("Please enter Product ID.");
            f.prod_id_chk.focus();
            return false;
        }
    
        else if(f.prod_id.options[f.prod_id.selectedIndex].value != f.prod_id_chk.value )
        {
            alert("Please enter same Product ID.");
            f.prod_id_chk.focus();
            return false;
        } 
        
        else if(f.add_manu_prod_id.value == "")
        {
            alert("Please enter Manufacturer ID.");
            f.add_manu_prod_id.focus();
            return false;
        }
        return true;
    }
    ...
    <form action="insert_add_prod.asp" method="post" name="add_prod" id="add_prod" onsubmit="return ValidateFields(this)">


  •  

    Posting Permissions

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