Skip to content

Commit

Permalink
Merge pull request #505 from UW-GAC/feature/maxlength-copy-paste
Browse files Browse the repository at this point in the history
Check copy-paste text in form fields and alert if it exceeds maxlength
  • Loading branch information
amstilp authored Mar 22, 2024
2 parents e0cd578 + d5a72c2 commit 636f460
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions primed/static/js/project.js
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
/* Project specific Javascript goes here. */

// Handle paste event for text inputs with maxlength.
const checkPasteLength = (e) => {
var paste = (e.clipboardData || window.clipboardData).getData("text");
maxlength = e.target.getAttribute("maxlength");
if (paste.length > maxlength) {
alert("String longer than allowed maximum length of " + maxlength + " characters:\n" + paste)
e.preventDefault()
e.stopPropagation()
}
}

var textInputs = $('form').find("input[maxlength]")
// textInputs.on("paste", checkPasteLength);
for(var i = 0; i < textInputs.length; i++){
// Console: print the clicked <p> element
textInputs[i].addEventListener("paste", checkPasteLength);
}

0 comments on commit 636f460

Please sign in to comment.