4

I've got a simple form where the user enters his contact number. If the contact number starts with 07 then the checkbox is enabled anything else I need it to be disabled.

I have written some code but the problem I face is when the user types 01 then it's disabled but if they then go on to add any other numbers after the 01 the checkbox becomes enabled.

function deselectcheckbox(wildvalue) {
  if (document.getElementById("1").value == "01") {
   $("#checkbox").attr("disabled", true);
   document.getElementById("divText").innerHTML = "Not able to send";
  }
  else
   {
   $("#checkbox").attr("disabled", false);
   document.getElementById("divText").innerHTML = "Send text";
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table>
<tr>
 <td>Contact Number</td>
 <td><input type="text" id="1" name="1" onkeyup="deselectcheckbox(this.value)"></td>
</tr>
<tr>
 <td><label><div id="divText">Send text</div></label></td>
 <td><input type="checkbox" name="contact" id="checkbox" value=""></td>
</tr>
</table>

I've tried adding a wildcard to

(document.getElementById("1").value == "01*")

But it's not working, Please help

2
  • Why would a wildcard help with an exact comparison..? Commented May 22, 2018 at 15:54
  • 2
    Change your comparisone to use a substring of the first 2 digits Commented May 22, 2018 at 15:54

1 Answer 1

5

If you only want the checkbox to be enabled when the content starts with "07" you can check for that first and disable inside of the else block.

  if (document.getElementById("1").value.startsWith("07")) {
    $("#checkbox").attr("disabled", false);
    document.getElementById("divText").innerHTML = "Send text";
  }
  else
  {
    $("#checkbox").attr("disabled", true);
    document.getElementById("divText").innerHTML = "Not able to send";
  }
Sign up to request clarification or add additional context in comments.

1 Comment

@RickKap If it solved your problem, don't forget to mark it as an answer! ✅

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.