...

View Full Version : Compare dropdown value and textbox value



ashok_19
10-17-2003, 07:18 AM
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.

Roelf
10-17-2003, 08:50 AM
looks like it should work, do the options have a value attribute?

glenngv
10-17-2003, 11:07 AM
...or are you calling the function in the form onsubmit handler or onclick of a submit button?


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:


<form name="add_prod" action="..." method="post">
...
<input type="submit" value="Submit" onclick="return validate(this.form)">
</form>


The 1st method is recommended, though...

ashok_19
10-17-2003, 08:06 PM
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;

}

Roelf
10-18-2003, 07:33 AM
answered in the other thread you started about this exact same subject
(saw that one before this one)

glenngv
10-18-2003, 04:10 PM
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?

ashok_19
10-19-2003, 12:23 AM
<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>

glenngv
10-20-2003, 03:32 AM
You should put return false if blank entry is encountered and return true at the end.



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:


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)">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum