Skip to content

Instantly share code, notes, and snippets.

@coulterpeterson
Last active June 2, 2023 18:32
Show Gist options
  • Save coulterpeterson/526b3b64b5b85c1fd8d633b847f91e88 to your computer and use it in GitHub Desktop.
Save coulterpeterson/526b3b64b5b85c1fd8d633b847f91e88 to your computer and use it in GitHub Desktop.
Gravity Forms | Restrict Number Field to X Number of 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