Skip to content

Instantly share code, notes, and snippets.

@Joaquim3
Created July 30, 2024 11:50
Show Gist options
  • Select an option

  • Save Joaquim3/681e0575d021eedb8528a7d634209cd6 to your computer and use it in GitHub Desktop.

Select an option

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
<!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