need a regex pattern to restrict the user to type only a integer
Application Type
Reactive

Need a regex pattern to restrict the user to type only a integer  ,

I USED the javascript code in the picture i uploaded it had a minor bug , 

and the bug is it takes the first  text input  but in my case it does not allow even a single text input


kindly suggest me any solution 


THANKS IN ADVANCE

Hi,

If you are using querySelectorAll then you need to use below code -


var all = document.querySelectorAll('.mobile');

Array.from({length: all.length}, (v, k) => k).forEach((index) => {
     let element = all.item(index);
element.addEventListener('input', restrictNumber);

});

function restrictNumber (e) {  

  var newValue = this.value.replace(new RegExp(/[^0-9\.]/g,'ig'), "");

  this.value = newValue;

}




Your code is for ES6 so i tried Above code which is in ES5 it works fine    

  

Hi,

Please add below Javascript in your screen "OnRender" event -


var mobile = document.querySelector('.mobile');

mobile.addEventListener('input', restrictNumber);

function restrictNumber (e) {  

  var newValue = this.value.replace(new RegExp(/[^0-9\.]/g,'ig'), "");

  this.value = newValue;

}


Thanks

Vinod

I need to do it for almost 10 input widget with same class ,but your code worked only for one input widget in same class so i tried replacing querySelector To querySelectorAll in javascript code  it says mobile.addEventListener is not a function       

               An suggestion .......   

                                THANKS IN ADVANCE 

Hi,

If you are using querySelectorAll then you need to use below code -


var all = document.querySelectorAll('.mobile');

Array.from({length: all.length}, (v, k) => k).forEach((index) => {
     let element = all.item(index);
element.addEventListener('input', restrictNumber);

});

function restrictNumber (e) {  

  var newValue = this.value.replace(new RegExp(/[^0-9\.]/g,'ig'), "");

  this.value = newValue;

}




Your code is for ES6 so i tried Above code which is in ES5 it works fine    

  

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.