Make the input edge flash

Asked

Viewed 473 times

0

I’d like to signal when the input of a form be wrong.

I’m wearing jQuery. In my head I imagined this:

if ($("#campo1").val() < 0){
    var i;
    for (i = 0; i < 10; i++) {
        $('#campo2').attr('style', 'border-color: red');
        sleep(1000);
        $('#campo2').attr('style', 'border-color: #cccccc');
        sleep(1000);
    }
}

But it is not working. Certainly in practice it should not be so.

  • Wrong? What would be wrong? Empty? Try to be more specific.

  • Surely lack the width of the edge think the attribute is border-weigth.

  • @sant0will This is the driving if ($("#campo1").val() < 0){

  • @Viniciusdejesus It turns red, but does not blink, as the original idea. So it means that my code is not the correct way to do this.

  • Ahhh got it wrong, it wouldn’t be the one second?

  • maybe you should slow down the time

  • Interested in a CSS-only answer?

Show 2 more comments

1 answer

1


With for you cannot do this. You could use a recursive function in this way:

var temp1, temp2;
if ($("#campo1").val() < 0){
   let i = 0;
   function pisca(){
      $('#campo2').css('border-color', 'red');
      if(i < 10){
         temp1 = setTimeout(function(){
            $('#campo2').css('border-color', '#ccc');
            temp2 = setTimeout(pisca, 500);
            i++;
         }, 500);
      }
   }
   pisca();
}

$("#campo1").on("input", function(){

   if(this.value > -1){
      clearTimeout(temp1);
      clearTimeout(temp2);
   }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="campo1" type="number" value="-1">
<input id="campo2" type="text">

  • How to stop blinking when the person enters a POSITIVE value?

  • Edited response.

  • It stopped not...

  • See now rs...

  • Show, thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.