Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-17-2003, 06:18 AM   PM User | #1
ashok_19
New to the CF scene

 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ashok_19 is an unknown quantity at this point
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.
ashok_19 is offline   Reply With Quote
Old 10-17-2003, 07:50 AM   PM User | #2
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
looks like it should work, do the options have a value attribute?
Roelf is offline   Reply With Quote
Old 10-17-2003, 10:07 AM   PM User | #3
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
...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...
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 10-17-2003, 07:06 PM   PM User | #4
ashok_19
New to the CF scene

 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ashok_19 is an unknown quantity at this point
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;

}
ashok_19 is offline   Reply With Quote
Old 10-18-2003, 06:33 AM   PM User | #5
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
answered in the other thread you started about this exact same subject
(saw that one before this one)
Roelf is offline   Reply With Quote
Old 10-18-2003, 03:10 PM   PM User | #6
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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?
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 10-18-2003, 11:23 PM   PM User | #7
ashok_19
New to the CF scene

 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ashok_19 is an unknown quantity at this point
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>
ashok_19 is offline   Reply With Quote
Old 10-20-2003, 02:32 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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)">
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:47 PM.


Advertisement
Log in to turn off these ads.