2

I have an input field like this

<input type ="number"   id="numberTxt" placeholder="Number Only"/>

I am checking whether the value is a number or not and clearing back as the following function using keypress event

    <script>
       $(document).ready(function(numberTxt){
          $('#numberTxt').keypress(function(event) {  

              var charCode = (event.which) ? event.which : event.keyCode;
              if (charCode != 45 && (charCode != 46 || $(this).val().indexOf('.') != -1) && (charCode < 48 || charCode > 57))
              {
                   return false; 
              }
              else 
                   return true;

          }); 
       });
    </script>

BUT when we copy paste some text (CTRL V CNTRL C) the above code is not working

or that i tried this function below but not getting the value of the input at that point its "" (empty string). when we copy paste its correctly triggering this function.

<script>
           $(document).ready(function(){
              $('#numberTxt').on("paste",function('numberTxt') {  

                 var val = $('#numberTxt').val() ;  

              }); 
           });
</script>

Is the any another way to get the input tag's value with event , if i pass the event like the keypress function before

any other Suggestions or answers to check number during copy paste will also be helpful

Reshma
  • 492
  • 3
  • 9
  • 23
  • 2
    http://stackoverflow.com/questions/8747439/detecting-value-change-of-inputtype-text-in-jquery – Pat Dobson Nov 28 '13 at 10:36
  • Note that you'll probably want to allow for drag'n'drop too. Or you could just remove any unwanted characters in a `blur` handler... If using jQuery you can safely use `event.which` - no need for the test and the `event.keyCode` fallback because jQuery normalises `event.which`. – nnnnnn Nov 28 '13 at 10:37
  • Can i get textbox value with event? – Reshma Nov 28 '13 at 10:41

2 Answers2

2

One might well feel below function correct using .bind('input'.....

http://jsfiddle.net/pxfunc/KDLjf/ got this one from

Is it possible to get pasted text without using the setTimeout() function?

Community
  • 1
  • 1
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
1

This is how you validate only integer value on PASTE

$('#numberTxt').on("paste",function(e) {  
    var $this = $(this);
    setTimeout(function(){
        var val = $this.val(), 
            regex = /^[\d]+$/;
        if( regex.test(val) ){
            $this.val( val );
        }
        else{
            $this.val('');
        }
      alert(val);    
    },0);

});

DEMO: http://jsfiddle.net/jogesh_pi/LaN8f/1/

jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • I need a way without settimeout. – Reshma Nov 28 '13 at 11:14
  • @Reshma what's the problem with `settimeout()` – jogesh_pi Nov 28 '13 at 11:15
  • i need to do a filter with that textbox and do a set of filling of more that 10 textbox and html according to the value in the text box. and need to do these filtering with a little bit more amount of data in 3 to 4 arrays. timing is really a issue for me . so i need to know any faster way . thats all. – Reshma Nov 28 '13 at 11:18