I have a page with a huge form, many input fields. Sometimes our customers copy paste their info into this form and in the process, some whitespace are pasted into the form.
I want to be able to remove ALL whitespace before and after the values they put in the form fields.
For instance, I want ' some thing '
to become 'some thing'
.
Preferably I want to trigger this stripping when they leave the form field, that is, on blur.
How can I do this with jQuery or vanilla JS?
Use String#trim()
to remove leading and trailing spaces.
$('form').on('blur', 'input[type="text"], textarea', function() {
// ES6
// $(this).val((i, value) => value.trim());
// ES5
$(this).val(function(i, value) {
return value.trim();
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.