JavaScript How to restrict input textbox to enter numbers only in Angular2 form? restrict textbox input to numbers c#,restrict textbox input to

I have to restrict user to enter only numbers in a field in Angular2->form I have solution but backspace is not working in input field Can anybody have proper solution for that?

form.html

<input (keypress)="keyPress($event)" minlength="8" maxlength="15" required />

form.component.ts

keyPress(event: any) {
    const pattern = /[0-9\+\-\ ]/;
    let inputChar = String.fromCharCode(event.charCode);
    // console.log(inputChar, e.charCode);
       if (!pattern.test(inputChar)) {
       // invalid character, prevent input
           event.preventDefault();
      }
 }

On kepress event it restrict user to enter only numbers but problem with this code is backspace, tab keys are not working.So, this code is not as per my expectation...

Answer:1

You can achieve that using regex the following way:

var numbersOnly = document.getElementById("numbersOnly");
numbersOnly.onkeyup = function myFunction() {
    numbersOnly.value = numbersOnly.value.replace(/[^0-9]/g, '');
};
<input id="numbersOnly" minlength="8" maxlength="15" required>
Answer:2

How about using the html5 tag? <input type="number">
It 's supported from all browsers.

https://www.w3schools.com/html/html_form_input_types.asp

Answer:3

you can use this way too

keyPress(event: any) {

        const pattern = /[0-9\+\-\ ]/;
        let inputChar = String.fromCharCode(event.charCode);

            if (!pattern.test(inputChar) && event.charCode != '0') {
                event.preventDefault();
            }
    }
Answer:4

Backspace will not work because: event.charCode will not map to one of the digits 0-9 for the backspace key.

You need to whitelist the backspace (i.e. keyCode = 8). Something like this (off the top of my head):

if(event.keyCode != 8 && !pattern.test(inputChar)) {
   event.preventDefault();
}
Answer:5

my answer is :- form.html:

form.component.ts

restrictNumeric = function (e) {
     var input;
     if (e.metaKey || e.ctrlKey) {
        return true;
     }
     if (e.which === 32) {
        return false;
     }
     if (e.which === 0) {
        return true;
     }
     if (e.which < 33) {
        return true;
     }
     input = String.fromCharCode(e.which);
     return !!/[\d\s]/.test(input);
 }
Answer:6

I know JQuery is a code smell in ReactJS because it traverses the entire DOM to do it's work. But, I've found some places where it's hard NOT to use it. This is one instance where I'd like to find a ...

I know JQuery is a code smell in ReactJS because it traverses the entire DOM to do it's work. But, I've found some places where it's hard NOT to use it. This is one instance where I'd like to find a ...

I'm kind of stuck here in this situation, where I need my app to reload when changing from one "project" to another. I've tried many things and I have been searching over and over to find a solution. ...

I'm kind of stuck here in this situation, where I need my app to reload when changing from one "project" to another. I've tried many things and I have been searching over and over to find a solution. ...

  1. angularjs ui-sref-opts

As explained in mdn, custom elements may be based on a native element. For Example, <button is="my-button">. What I'm looking for is kind of the opposite syntax. I'd like a shorthand for ...

As explained in mdn, custom elements may be based on a native element. For Example, <button is="my-button">. What I'm looking for is kind of the opposite syntax. I'd like a shorthand for ...

I'm using the awesome Slick slide on a project. The problem is: I have a carousel with 5 divs inside. 4 images and a video. On mobile (< 768px) I want the slider to show only the images, not the ...

I'm using the awesome Slick slide on a project. The problem is: I have a carousel with 5 divs inside. 4 images and a video. On mobile (< 768px) I want the slider to show only the images, not the ...

  1. remove slick slider jquery
  2. remove slick slider
  3. slick remove slide
  4. slick remove slide by class
  5. slick remove first slide
  6. slick remove last slide
  7. slick slider remove arrows
  8. slick slider remove slide
  9. slick slider remove clone
  10. slick slider remove outline
  11. slick slider dots remove numbers
  12. slick slider remove buttons
  13. slick slider remove arrow text
  14. slick slider remove blue outline
  15. slick slider remove dots
  16. slick slider remove tabindex
  17. slick slider remove draggable
  18. slick slider remove all slides
  19. slick slider remove transform
  20. slick slider remove animation