I want to remove first and last space of any input on all the events (keyup/keydown/oncopy/onpaste etc).
I have tried with below code but its not working.
$("body").on("input propertychange", "input", function (e) {
if (e.which === 32 && e.target.selectionStart === 0) {
return false;
}
});
You need to change your HTML :
function trim (el) {
el.value = el.value.
replace (/(^\s*)|(\s*$)/gi, ""). // removes leading and trailing spaces
replace (/[ ]{2,}/gi," "). // replaces multiple spaces with one space
replace (/\n +/,"\n"); // Removes spaces after newlines
return;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Enter value : <input type="text" name="test_input" onchange="return trim(this)" />
Would you please check my above snippet?