
I want to validate textbox based on dropdown menu consisting of many options. For eg Dropdown consists of option (1,2,3,4) if "1" is selected textbox should validate a value 0f "2300" through a submit button "Evaluate". Please give me the sample code for the above problem using only javascript
Thanks for the code.. it worked well.. I have made some modifications. The problem with the code is as soon as we click the button "Evaluate" it evaluates as per the input in dropdown menu nd text box and shows an alert box of "correct" or "incorrect" according to inputs.But as we click "ok" on alert box the input values entered gets reseted and gets vanished. I don't want the entered values getting reseted. Also when either or both of the inputs i.e drop down menu and textbox are missing it should show an alert box indicating "Please fill in th options" which I am not able to implement. Can you please make the required modification in the following program Thank you
<HTML>
<SCRIPT>
function validate()
{
var v=document.getElementById("t").value;
var e = document.getElementById("sel");
var st = e.options[e.selectedIndex].text;
if(e=="" && v=="")
{
alert("Please fill in the options.");
}
else if(st=="1"&& v=="2300")
{
alert("Correct answer");
}
else
{
alert("Incorrect answer");
}
}
}
</SCRIPT>
</HTML>
<form name="f" onsubmit="return validate();">
<pre>
<select id="sel">
<option value="0">Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="text" id="t">
<input type="submit" value="submit">
</html>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.