0

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();
        }

    });
});
3
  • Let us know what exactly you want to do? This piece of code if ($(this).val() == "M") { if ($('input[name=EmailContact]').val() == "") { alert("Empty"); return false; } } checks if the selected value is "M" ie Email has value or not. Since no value is present, the Alert comes Commented Sep 4, 2018 at 8:31
  • did you try do when form is submitting? Commented Sep 4, 2018 at 8:36
  • @M3ghana Yes , when i selected E-mail from DDL , it display a textbox and if textbox is empty display alert(); Commented Sep 4, 2018 at 8:37

3 Answers 3

1

Try this -

Attach the button with click event -

HTML

<!DOCTYPE html>
<HTML>
    <HEAD>
    </HEAD>
    <BODY>
        <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">
                </div>
         <button id="btnSubmitRMA" type="submit" class="btn">Submit</button>

            </div>
        </div> 
    </BODY>
</HTML>

JS

Click function is triggered on click of the button

$(document).ready(function ()
{
       $("#btnSubmitRMA").click(function (e) {
                e.preventDefault();
            });

      $("#btnSubmitRMA").click(function () {
                    if ($("#ddlsmsemailboth").val() == "M") {   
                        if ($('input[name=EmailContact]').val() == "") {
                            alert("Empty");
                            return false;
                        }
                    } 
            });

$(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();
        }

    });
});
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hi friend , the coming another guy before u and give right answer:) , but your answer was helpful and useful i want to mark as useful answer, but i dont have enough reputation as soon i get , i will mark it as useful answer , i hope its ok :) have great day :)
0

you can change your function RMAfunction() to:

function RMAfunction() {
    if ($('#ddlsmsemailboth').val() == "M") {   
        if ($('input[name=EmailContact]').val() == "") {
            alert("Empty");
            return false;
        }
    } 
}

Comments

0

You can modify your RMAfunction() like, and ensure form tag should be there in your html

    $(document).ready(function () {

         $("#btnSubmitRMA").click(function (e) {
            e.preventDefault();
            RMAfunction();
           });
    });

    function RMAfunction() {

        if($("#ddlsmsemailboth option:selected").val() == "M" 
           && $('input[name=EmailContact]').val() == "" ){
            alert("you forgot enter you email");
            return false;
        }
      } 

2 Comments

i tried dat to , its also working fine, thanks for help :)
please make $(document).ready function and put click function inside it.and you used $(function () { also, both r same for uniqueness you can use either one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.