0

I have Input type number, and when I write 123- I get an empty value from event.target.value, but I want get value as I write in the input and replace the value with regex /(^([^-\d]*)|[^\d]*)/g for empty value. It is possible to do it? How to get an exactly the same text from input?

It must be a type number because sometimes I need to use min and max attributes.

I want replace text via regex like this (value.replace(regex, ''):

  • -123.123 into -123123
  • ---123-456 into -123456
  • 123-45 into 12345
  • 12e23,.- into 1223

const myFun = (e) => 
  setTimeout(()=>{
    console.log(e.target.value)}
  )
<input onkeydown="myFun(event)" type="number"/>

originally i use angular 8 and host listener

@HostListener('input', ['$event']) // or/and keydown
onInputChange(event: Event): void {...}
Kordrad
  • 1,154
  • 7
  • 18
  • _"I want replace text via regex..."_ - Why? If the browser supports `type="number"` it will only allow valid numbers to be submitted. – Andreas Sep 11 '20 at 07:40
  • I just want to block out unwanted characters from the input and paste stage. – Kordrad Sep 11 '20 at 07:44
  • The browser already does this for you. When the form is submitted it will check the content of the field for validity. If you need that check to happen before actually submitting the form then [trigger it by hand](https://stackoverflow.com/questions/7548612/triggering-html5-form-validation) – Andreas Sep 11 '20 at 07:48
  • I know this, but I'd like to know if I could go a step further and manipulate the value of input[type="number"]. As for input[type="text"] everything would work as intended, but I can't use min & max then – Kordrad Sep 11 '20 at 07:51

0 Answers0