1

We are using MVC popup model where having two text box SSN number and DOB(Date of birth), i am using datepicker calendar for DOB and it is read only text box, for SSN number i am manually put the number in text box. i have written the below code to enable next button when SSN number and DOB both are correct. But below code is not working properly, First i put the SSN number and When i Select the DOB using calendar, next button is not enable, Please help me to resolve this issue.

$("#SocialSecurityNumber").blur(function () {

        if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
            $("#nextSubmit").removeAttr("disabled");
            $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
        }
        else {

            $("#nextSubmit").attr("disabled", true);
            $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

        }

    });

    $("#SocialSecurityNumber").keyup(function () {
        if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
            $("#nextSubmit").removeAttr("disabled");
            $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
        }
        else {

            $("#nextSubmit").attr("disabled", true);
            $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

        }

    });


    $("#DateofBirth").blur(function () {

        if ($("#DateofBirth").val() !='' && $("#SocialSecurityNumber").val().length == 11) {
            $("#nextSubmit").removeAttr("disabled");
            $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
        }
        else {

            $("#nextSubmit").attr("disabled",true);
            $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

        }

    });

    $("#DateofBirth").keyup(function () {
        if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
            $("#nextSubmit").removeAttr("disabled");
            $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
        }

        else {

            $("#nextSubmit").attr("disabled",true);
            $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

        }
    });

Thanks, Sandeep

4
  • please comment this. so we can find out what do you want to do in these statements Commented Jun 15, 2018 at 5:22
  • 1
    can you please share your HTML? and as for what code you have written i think these are the scenarios: 1. if user only enter the SSN next button will not be enabled as the DOB field is empty. 2.If the user had entered SSN and enter the DOB(not select the date by any plugin as keyup and blur will not work but onChange might) then the next button will be enabled. Commented Jun 15, 2018 at 5:23
  • Please find below my HTML. Commented Jun 15, 2018 at 5:34
  • Please find below my HTML. Html.TextBoxFor(m => m.DateofBirth, new { class = "DateofBirth form-control txtWithImage", placeholder = "_ / / _ _ _", readonly = "readonly" }) Commented Jun 15, 2018 at 5:56

3 Answers 3

2

in jquery this is how you should disable a button

$('#id_of_the_button').prop('disabled', true);

to enable the button

$('#pc_add').prop('disabled', false);

in your html the button should be like this

<button id="id_of_the_button"> text of the button </button>

To answer your question here how you should implement your html file

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js">
</script>
<script>
$(document).ready(function(){
     $("#SocialSecurityNumber").keyup(function() {
     if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
        $("#nextSubmit").prop('disabled', false);
      } else {
        $("#nextSubmit").prop('disabled', true);
      }
    });

    $('#DateofBirth').change(function() {
         if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
         $("#nextSubmit").prop('disabled', false);
        }else {
            $("#nextSubmit").prop('disabled', true);
        }
    });
});


</script>
</head>
<body>

<h2>This is a heading</h2>
  <form>
    <input type="text" id="SocialSecurityNumber" />
    <input type="date" id="DateofBirth" />
    <button id="nextSubmit" disabled="disabled" class="btn btn-default">Submit</button>
  </form>

</body>

</html>
Sign up to request clarification or add additional context in comments.

3 Comments

Please find below my HTML. Html.TextBoxFor(m => m.DateofBirth, new { class = "DateofBirth form-control txtWithImage", placeholder = "_ / / _ _ _", readonly = "readonly" })
i have tried the your code but no luck, When i click on DOB text box second time then next button is enable.
please check again I have added full script
0

Instead of using blur and keyup events, use the change event on input.

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

  <!-- jQuery library -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <!-- Latest compiled JavaScript -->
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <form>
    <input type="text" id="SocialSecurityNumber" />
    <input type="date" id="DateofBirth" />
    <button id="nextSubmit" disabled="disabled" class="btn btn-default">Submit</button>
  </form>
  <script>
    $("#SocialSecurityNumber").change(function() {
      if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
        $("#nextSubmit").removeAttr("disabled");
        $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
      } else {

        $("#nextSubmit").attr("disabled", true);
        $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

      }

    });


    $("#DateofBirth").change(function() {

      if ($("#DateofBirth").val() != '' && $("#SocialSecurityNumber").val().length == 11) {
        $("#nextSubmit").removeAttr("disabled");
        $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
      } else {

        $("#nextSubmit").attr("disabled", true);
        $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

      }

    });

    $("#DateofBirth").keyup(function() {
      if ($("#SocialSecurityNumber").val().length == 11 && $("#DateofBirth").val() != '') {
        $("#nextSubmit").removeAttr("disabled");
        $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");
      } else {

        $("#nextSubmit").attr("disabled", true);
        $("#nextSubmit").removeClass("btn-primary").addClass("btn-default");

      }
    });
  </script>

  <body>

3 Comments

i have fixed this issue, in datepicker function i have call the onchange event which fixed the issue. thanks for your help.
oh okay. Thanks :)
Hi All, i have fixed this issue, in datepicker function i have write the on change event which fixed the issue. Please find below code, thanks for your help. .on('changeDate', function (ev) { if ($("#DateofBirth").val() == '' && $("#SocialSecurityNumber").val().length == 11) { var newDate = new Date(ev.date) newDate.setDate(newDate.getDate() + 1); $("#nextSubmit").removeAttr("disabled"); $("#nextSubmit").removeClass("btn-default").addClass("btn-primary");}
0

Try:

<!-- To disable button -->
$('#ElememtId').prop("disabled",true)

and

<!-- To enable button -->
$('#ElememtId').prop("disabled",false)

Comments

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.