简体   繁体   中英

Restrict the user to write more than 2 decimals in input

I'm working on a html form, and I like to know if there is a tool or way to restrict the user to some rules in inputs, ie: -Don't allow the user to put more than 2 digits after the dot -Don't allow the user to put more than 1 dot

I was thinking on regular expressions but how to apply that live.

Thank you.

You can run a form validation process with JavaScript. Use HTML DOM to get all the input fields, then use string manipulation to search it's "value" attribute for characters.

document.getElementById() and similar methods can retrieve different elements on the page. After the getElement method you used to find the input field, type ".value" and then it should return whatever is in that field. Then run that through something like data .includes() or similar to check for certain characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM