javascript

limit Input text box charecters ,enter only 10 characters at max

<script>
var keycount =0;
jQuery(document).on('keydown', 'input.product-custom-option', function(ev) {
    var c = ev.which;
   // console.log(c);
    if(c==8)
        {
        keycount--;
        }
    else{ 
        keycount++;
       // console.log(keycount);
    }
    if(keycount>10)
     {
	    return false;
     }
});
</script>

<input type="text" class="product-custom-option" />

OPtion 2

function ohmyfun()
{
var xdata='';
var lee = document.getElementsByClassName("product-custom-option")[0].id;
console.log(lee);
 document.getElementsByClassName("product-custom-option")[0].addEventListener("keydown", getleecount);
  
}
function getleecount()
{
      var lee_id = document.getElementsByClassName("product-custom-option")[0].id;
    var xle= document.getElementById(lee_id).value;
    
    console.log(xle.length);
    if(xle.length==9){
    
    xdata = document.getElementById(lee_id).value;
  console.log(xdata);
     document.getElementById(lee_id).value=xdata;
         
    }
    else if(xle.length>9){
        
         console.log(xdata);
         document.getElementById(lee_id).value=xdata;
    }
}
</script>

Leave a Reply

Your email address will not be published.

2 + 5 =