Last active
June 2, 2023 18:32
-
-
Save coulterpeterson/526b3b64b5b85c1fd8d633b847f91e88 to your computer and use it in GitHub Desktop.
Gravity Forms | Restrict Number Field to X Number of Characters
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
/** | |
* Gravity Wiz // Gravity Forms // Restrict Number Field to X Number of Characters | |
* https://gravitywiz.com/ | |
* | |
* Instructions: | |
* | |
* 1. Install this snippet with our free Custom JavaScript plugin. | |
* https://gravitywiz.com/gravity-forms-custom-javascript/ | |
* 2. Replace "1" with the ID of your number field, and "4" with the number of characters you want to limit it to. | |
* | |
*/ | |
// When the page is ready | |
window.addEventListener('load', function () { | |
if (document.querySelector('body') !== null) { | |
let characterLimit = 4; | |
let numberField = document.querySelector('#input_GFFORMID_1'); | |
// Credit: https://stackoverflow.com/a/34641129 | |
numberField.setAttribute("maxlength", characterLimit); | |
numberField.setAttribute("oninput", "javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment