I'm trying validate TextBox based on DropDownList selection , which means if user select E-mail or SMS or both from DropDownList it display TextBoxs based what user selected.lets say their select E-mail and it display a textbox to user enter their E-mail and lets say their forgot to Enter E-mail adresse (left it empty) and then click button , here i want display alert ("you forgot enter you email").
For this i did as below , but the problem is when user select E-mail from dropdown it display alert, which i dont wish for, i want when user selected E-mail from DDL , it display a textbox and if textbox is empty display alert(), so what did i wrong ! Can anyone please help me or point me in the right direction! :)
Thanks in advance.
HTML:
<div class="col-md-6">
<div class="form-group">
<select class="form-control border-input" id="ddlsmsemailboth" name="ddlsmsemailboth">
<option value="">- Select -</option>
<option value="S">SMS</option>
<option value="M">E-mail</option>
<option value="B">SMS/Email</option>
</select>
<div id="dvkemail" style="display: none">
<label class="form-control-label">Enter E-mail</label>
<input name="EmailContact" id="EmailContact" type="text" class="form-control border-input">
</div>
<div id="dvksms" style="display: none">
<label class="form-control-label">Enter phone number </label>
<input name="Telefonnummer" id="Telefonnummer" type="tel" placeholder="4512345678" class="form-control border-input">
</div>
<div id="dvkboth" style="display: none">
<label class="form-control-label">Enter phone number </label>
<input name="Telefonnummer" id="Telefonnummer" type="tel" placeholder="4512345678" class="form-control border-input">
<label class="form-control-label">Enter E-mail</label>
<input name="EmailContact" id="EmailContact" type="text" class="form-control border-input">
<button id="btnSubmitRMA" type="submit" class="btn">Submit</button>
</div>
</div>
</div>
JavaScript:
$("#btnSubmitRMA").click(function (e) {
e.preventDefault();
RMAfunction();
});
function RMAfunction() {
$("#ddlsmsemailboth").change(function () {
if ($(this).val() == "M") {
if ($('input[name=EmailContact]').val() == "") {
alert("Empty");
return false;
}
}
});
});
JavaScript (for hide/show texbox based on dropdown selected value):
$(function () {
$("#ddlsmsemailboth").change(function () {
if ($(this).val() == "S") {
$("#dvksms").show();
$("#dvkemail").hide();
$("#dvkboth").hide();
}
else if ($(this).val() == "M") {
$("#dvkemail").show();
$("#dvksms").hide();
$("#dvkboth").hide();
}
else if ($(this).val() == "B") {
$("#dvkboth").show();
$("#dvkemail").hide();
$("#dvksms").hide();
}
else {
$("#dvkboth").hide();
$("#dvkemail").hide();
$("#dvksms").hide();
}
});
});