Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a javascript function on focus out

I have a javascript function:

    function testCreditCard () {
    myCardNo = document.getElementById('ccnumber').value;
    myCardType = document.getElementById('ccType').value;
    if (checkCreditCard (myCardNo,myCardType)) {
        alert ("Credit card has a valid format")
    } 
    else {
        alert (ccErrors[ccErrorNo])
        };
    }

and an input field <input type="text" id="ccnumber" />

and the script function is triggered using an onclick function

<button type="post" id="btn_c" name="btn_c" onclick="testCreditCard();" >Click</button>

Is there anyway I could trigger the function on focus out/right after filling the input field?

Im new to javascript / jquery.Please suggest a solution for this.Thank You

Tried this

<input type="text" id="ccnumber" />
<script> //Script for testCreditCard </script>
    <script>$("#ccnumber").on("change", testCreditCard);</script>
    <script>
    function testCreditCard () {
      myCardNo = document.getElementById('ccnumber').value;
      myCardType = document.getElementById('ccType').value;
      if (checkCreditCard (myCardNo,myCardType)) {
      } 
      else {alert (ccErrors[ccErrorNo])};
    }
    </script>

</body>

But didnt work.

like image 243
Piya Avatar asked Apr 01 '26 06:04

Piya


2 Answers

Bind to the change or blur event

$("#ccnumber").on("change", testCreditCard);
like image 177
epascarello Avatar answered Apr 03 '26 21:04

epascarello


why you don't try to call onblur property of the input text?

    <input type="text" onblur="testCreditCard()" id="id" name="id" />

when the focus is out of the text the js function is activated

my apologies for my english

like image 44
clairerb6 Avatar answered Apr 03 '26 21:04

clairerb6



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!