how can get only numbers from the input

93
December 02, 2019, at 11:40 PM

I have on keyup and mask for phone numbers, I need to get only digits, not symbols on mask, but it is not working, please let me know my mistake.

$(document).on('keyup', '#search', function(){
    sd=parseInt(document.getElementById('search').value);
    alert(sd);

'$(window).load(function()
{
   var phones = [{ "mask": "(###) ###-####"}, { "mask": "(###) ###-####"}];
    $('#search').inputmask({ 
        mask: phones, 
        greedy: true, 
        definitions: { '#': { validator: "[0-9]", cardinality: 1}} });
});'
Answer 1

I might recommend doing a replace with a regular expression to remove all non-digits:

const sd = document.getElementById('search').value.replace(/\D/g,''); 
console.log(sd);
<input id="search" value="(123) 456 - 7890" />

Answer 2

You can use the unmask method provided by the inputmask library:

const phones = [{ "mask": "(###) ###-####"}, { "mask": "(###) ###-####"}];
const mask = { 
  mask: phones, 
  greedy: true, 
  definitions: { '#': { validator: "[0-9]", cardinality: 1}} 
}
$(document).on('keyup', '#search', function(){
  let value = document.getElementById('search').value;
  console.log( Inputmask.unmask( value, mask ) );
});
$('#search').inputmask( mask );

Codepen

Answer 3

With jQuery you can prevent inputs by returning false on the keydown event.

//48-57 are number keys, 96-105 are numpad keys, the rest are control keys [backspace, tab, enter, left, up , right, down, delete]
$('input').on('keydown', e => (e.keyCode >= 48 && e.keyCode <=57) || (e.keyCode >= 96 && e.keyCode <=105) || [8,9,13,37,38,39,40,46].includes(e.keyCode));

https://jsfiddle.net/un18rw96/

READ ALSO
CropperJS getCroppedCanvas() returns null on second initialization

CropperJS getCroppedCanvas() returns null on second initialization

The method is called in the ready eventOn the first call it works

75
How to change all url images in blogger with custom url images

How to change all url images in blogger with custom url images

how do I replace all the url images in bloggerexample bp1

62
Check if xml is different, either whole xml or just &lt;lastBuildDate&gt;

Check if xml is different, either whole xml or just <lastBuildDate>

I'm trying to verify if a XML document got modified after I loaded my page, like maybe every 30 secondsI obtain the xml by making an AJAX request, and preferably, I want to compare the new one and the previous one before calling a function that displays...

81