0

Aim : Disallow numeric values on an input Field

Logic: I am trying to replace every numeric value with empty string. But on every next keypress, the original val string is prepended to it.

For example :

---------------------------------------------------------
Key Entered          |         Value reflected in Input
---------------------------------------------------------
A                    |                A
b                    |                Ab
1                    |                Ab
c                    |                AbAbc
----------------------------------------------------------

Code :

HTML

 <input id="charText" value="" class="alpha">

Script

$('.alpha').on('keyup', function () {

    //$(this).val( $(this).val().replace(/[^A-Za-z]/g,'') );
    $val = $(this).val().replace(/[^A-Za-z]/g,'');;
    $('.alpha').val($val);
    });
});

Works for desktop. Error only in my Android mobile

cosmoloc
  • 2,884
  • 5
  • 29
  • 48

1 Answers1

0

I was able to solve the same using following script (Ref : JavaScript keypress event not raised on Android browser)

$(window).load(function(){
    $('.alpha').on('keyup input', function (event) {

        if(event.type == 'input') {
          var bufferValue = $(this).val().replace(/[^A-Za-z]/g,'');
          $(this).val(bufferValue);
        }

    });
});
cosmoloc
  • 2,884
  • 5
  • 29
  • 48