HTML5 input type=number value is empty in Webkit if has spaces or non-numeric characters?

后端 未结 5 1786
温柔的废话
温柔的废话 2020-12-01 15:39

This is strange behavior to me but on Webkit browsers (Chrome/Safari, not Firefox) if I include a space in a string of numbers in an

5条回答
  •  星月不相逢
    2020-12-01 16:19

    You're setting a numeric input field to a string which is not a number. What did you expect to happen? The whole point is that these fields don't allow or accept non-numeric input. They are documented as only accepting a floating point value.

    There is no "hack" available or required; the solution is to stop using a number input field for a value that isn't a number. Credit cards, phone numbers, etc; these things are not numbers. They contain digits as a subset of the valid characters, but they also contain completely non-numeric characters like spaces, hyphens and parenthesis. They need to be stored and treated as regular strings.

    Use .

提交回复
热议问题