0

my telephone number validation doesn't work and I don't know why. Submit should be active when number length is 9 or 10,in other case submit should be disabled.Here's the code

HTML body:

<body>
<input type="number" name="number-189" value="" class="example1" id="numertel" min="500000000" max="900000000" aria-invalid="false">
<input type="submit" value="Send" class="example" id="przycisk">
    <script src="script.js"></script>
</body>

JS

var numertell = document.getElementById("numertel");
var numerlength =  numertell.value.length;
if(numerlength==10 && numerlength==9)
  document.getElementById("przycisk").disabled=false;
else
  document.getElementById("przycisk").disabled=true;
6
  • 4
    What "doesn't work" about it? How is this code even invoked as the value changes? Commented Nov 2, 2017 at 11:42
  • make a working snippet Commented Nov 2, 2017 at 11:43
  • 1
    && == AND, || == OR Commented Nov 2, 2017 at 11:43
  • 1
    if(numerlength==10 && numerlength==9) will always be false. the length can't be 9 AND 10 in the same time. Change it to || Commented Nov 2, 2017 at 11:43
  • That's right,my obvious bad,but id doesn't work too.Submit is always disabled Commented Nov 2, 2017 at 11:48

5 Answers 5

7

Submit should be active when number length is 9 or 10,in other case submit should be disabled

Your problem is this line

if(numerlength==10 && numerlength==9)

replace && with || since numerlength can't be 9 and 10 at the same time. Check this doc to know more about logical operators.

if( numerlength== 10 || numerlength==9 )

Edit

You need to invoke the code share above when the value of textbox changes. I have shared an example with keyup event.

Demo

document.getElementById("przycisk").disabled = true;
document.getElementById("numertel").addEventListener("keyup", function() {
  var numerlength = this.value.length;
  //console.log( numerlength );
  if (numerlength == 10 || numerlength == 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});
<input type="number" name="number-189" value="" class="example1" id="numertel" min="500000000" max="900000000" aria-invalid="false">
<input type="submit" value="Send" class="example" id="przycisk">

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

3 Comments

Maybe explain the difference between && and ||? Other than that great answer.
Oh,that's right,but id doesn't work too.Submit is always disabled
@MiKcid I have added a demo.
0

Try this....

function check_value()
{
var numertell = document.getElementById("numertel");
var numerlength =  numertell.value.length;
if(numerlength == 10 || numerlength ==9)
  document.getElementById("przycisk").disabled=false;
else
  document.getElementById("przycisk").disabled=true;
 }
<body>
<input type="number" name="number-189" value="" class="example1" id="numertel" min="500000000" max="9000000000" aria-invalid="false" onblur = "check_value();">
<input type="submit" value="Send" class="example" id="przycisk">
    <script src="script.js"></script>
</body>

2 Comments

Thank you,that's working,but i'm very curious-can you tell me why my code isn't working?
the condition of numberlength should be "or" i.e "||"
0

html >>>

<body>
<input type="number" name="number-189" value="1234" class="example1" id="numertel" min="500000000" max="900000000" aria-invalid="false" onkeydown="myFunction()">
<input type="submit" value="Send" class="example" id="przycisk" >
    <script src="script.js"></script>
</body>

javascript >>>

$(document).ready(function(){
 myFunction();
});
function myFunction() {
     var numertell = document.getElementById("numertel");
  var numerlength =  numertell.value.length;
  if(numerlength==10 || numerlength==9)
    document.getElementById("przycisk").disabled=false;
  else
   document.getElementById("przycisk").disabled=true;
}

1 Comment

There is no indication from the user that they are using jQuery so there is no point in using jQuery's document ready when there are javascript only options.
0

You should also put that condition inside input event, and assign new numertell value inside that event, like this:

var numertell = document.getElementById("numertel");
var numerlength = numertell.value.length;
if (numerlength === 10 || numerlength === 9)
  document.getElementById("przycisk").disabled = false;
else
  document.getElementById("przycisk").disabled = true;

numertell.addEventListener('input', function(evt) {
  numerlength = numertell.value.length;

  if (numerlength === 10 || numerlength === 9)
    document.getElementById("przycisk").disabled = false;
  else
    document.getElementById("przycisk").disabled = true;
});
<body>
  <input type="number" name="number-189" value="" class="example1" id="numertel" min="500000000" max="900000000" aria-invalid="false">
  <input type="submit" value="Send" class="example" id="przycisk">
  <script src="script.js"></script>
</body>

Comments

0

Please try

if(numerlength === 10 || numerlength === 9).

|| is an operator for 'OR' operation in Javascript. Also for comparison should use === rather than ==.

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.