
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
<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>
Thanks for the code.. Worked well... According to the above code as soon as we select Evaluate button it shows alert box indicating Correct or Incorrect answer according to the input. But as we close the alert box the input values gets reseted to the original form. Can u please make the required modification? Also I want to implement a condition to show a alert box "Please fill in the option" when both or either of the inputs to drop down menu and textbox are missing. Please help me out with the code. Thank you
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.