5 ms·
Not being a front-end dev, it does seem trivial, but using [0-9\.]* means you can have 1.2.3. You can tighten that regex up, but I imagine that either doesn't f
by onei 4y ago
Not being a front-end dev, it does seem trivial, but using [0-9\.]* means you can have 1.2.3. You can tighten that regex up, but I imagine that either doesn't fly with mobile keyboard hinting or screen readers trying to hint what a correct value is.
- brundolf 4y agoThis one works: `[0-9]+([\.][0-9]+)?` > I imagine that either doesn't fly with mobile keyboard hinting or screen readers trying to hint what a correct value is The `pattern` attribute isn't used for anything except validation as far as I know; the mobile keyboard at least is entirely driven by `inputmode` and/or `type`
- greiskul 4y agoDoesn't work with i18n (some countries use , instead of . as the decimal separator).
- ascar 4y agoActually the majority of the countries use the comma. Though dot wins by population as China and India use it. https://en.m.wikipedia.org/wiki/Decimal_separator https://en.m.wikipedia.org/wiki/Decimal_separator
- darepublic 4y agoFwiw the pattern can be dynamic. But at that point as others have noted might be better to have a dedicated validator
- andylynch 4y agoIt will work until you have someone from Europe get in touch early in the morning to complain your form doesn’t accept decimal commas (although they may well just roll their eyes again and mutter something about how Anglos don’t bother supporting their international users properly).
- brundolf 4y ago`[0-9]+([\.,][0-9]+)?` then Getting into the weeds here though. The advantages/disadvantages of regex-based validation are beside the original point (and anyway I prefer doing validation in JS most of the time, for various reasons, but that's also out of scope)
- klibertp 4y ago> I prefer doing validation in JS most of the time As you should! :) Preferably by using a parser-generator library. Validation is basically just an error mode of parsing, and parsing things with regexes is not only unreadable, but also loses a lot of valuable information about the error.
- runarberg 4y agoLuckily it is easy to do validation with javascript if you properly set type=number: if (Number.isNaN(input.valueAsNumber)) { // If only this message could also by localized input.setCustomValidity("Please type a number"); } else { input.setCustomValidity(""); } But double luckily, we don’t even need to do this as we also get this for free, and as a bonus the message is localized.
- quink 4y agoSwitzerland is in Europe. With 1'000'000 percent certainty.