Hasibur Rahman
Hasibur Rahman

Reputation: 873

Why does HTML input type number accepts the + and - symbol?

Input type number accepts + and -. I don't want this behavior. How can I prevent this?

<input type="number">

I can type 100+ or 200- or both 300+- maybe some other special characters are acceptable. I don't want this. Number input should only be Number.

Is it possible User should not even type any special characters?

Upvotes: 1

Views: 941

Answers (1)

Quentin
Quentin

Reputation: 944441

The default behaviour of a number input is to allow the user to type anything they like and perform validation when the form is submitted.

This is consistent with how all other validation features built into HTML forms work.

If you type 100- then try to submit the form, the submission will fail and an prompt along the lines of "Please type a number" will appear.

If you want validate-as-you-type behaviour then you will need to implement it with JavaScript. This is tricky to pull off well though. For example, you might prevent a user from pasting a string copied from a document like 123,456.12 because it has a comma in it. If you let them paste it they can then edit the comma out.

Upvotes: 4

Related Questions