How to add the jQuery validation in textbox focusout event?

1.5k views Asked by At

How to add the validation to textbox control in focusout event only?

Does anyone have an idea?

2

There are 2 answers

2
Jarvis Stark On

$("#txt1").blur(function(){
  var v = $("#txt1").val();
  if(v == "test"){
    alert('valid');
  }
  else{
    alert('invalid');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
T1: <input type="text" id="txt1">
T2: <input type="text" id="txt2">

Refer: https://api.jquery.com/focusout/

.focusout(function() {

    // Put your validation code here
    if(!valid){
        return false;
    }    
})

OR

.blur(function() {

    // Put your validation code here
    if(!valid){
        return false;
    }    

})
0
Alexander Ilin On

Your html is:

<input type="text">

And your jQuery:

$('input').blur(function(){
    var text = $(this).val();
   //do smth with your text...
});