Created
July 30, 2024 11:50
-
-
Save Joaquim3/681e0575d021eedb8528a7d634209cd6 to your computer and use it in GitHub Desktop.
JAVASCRIPT ⇢ check for only digits, only 1 dot, only 2 digits after dot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <script> | |
| function isnumber (elem) { | |
| k = elem.value; | |
| // check for only digits | |
| // check for only 1 dot | |
| // check for only 2 digits after dot | |
| re = /^\d*(\.\d{0,2})?$/; | |
| if (re.test(k)==false) { | |
| // remove last typed char from string and set old value | |
| elem.value = k.substring(0, k.length - 1); | |
| }; | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <input id="test" oninput="return isnumber(this);" type="text"> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment