-2

I need to have a text input that accepts only numbers and the dot sign for float numbers.

I don't want a number type input.

naspy971
  • 1,137
  • 2
  • 13
  • 31
  • 2
    How is your current RegEx ? Why don't you want to use input type number ? – Cid Nov 29 '19 at 14:14
  • 2
    I didn't downvote your question, but that's like *"Let's ask before searching"* – Cid Nov 29 '19 at 14:20
  • Here's what I typed : "HTML pattern how to allow only numbers and dots", and here I am with no answer from Google ;) well anyway, I got my answer here, thanks – naspy971 Nov 29 '19 at 14:25
  • You could have typed *"regex float number"* – Cid Nov 29 '19 at 14:48

1 Answers1

-2

You can't do this with html only - you'd need to use JavaScript.

e.g. Javascript key board input filtering

There is an <input pattern=""> attribute which can be used for validation, but it does not prevent the input from accepting invalid characters, only prevents them being submitted when the form is validated.

Chris Wheeler
  • 1,623
  • 1
  • 11
  • 18
  • 2
    [The `pattern` attribute of `input` elements accepts regular expressions](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefpattern) – VLAZ Nov 29 '19 at 14:17
  • Oh wow, you're quite correct. It's well supported too - https://caniuse.com/#feat=input-pattern – Chris Wheeler Nov 29 '19 at 14:30
  • 1
    @VLAZ Having just tested this it doesn't prevent the input accepting things which don't match the pattern. It's only used for validation when the form is submitted. I therefore stand by my original answer - this requires javascript. – Chris Wheeler Nov 29 '19 at 14:35