簡単な解決策は、ここ$.trim
//removes leading and trailing spaces on every text field "on focus out"
$(":text").each(function(index) {
$(this).focusout(function() {
var text = $(this).val();
text = $.trim(text);
$(this).val(text);
});
});
====================
jqueryの関数を使用することであるが、単純なコードであります
//removes leading and trailing spaces on every text field "on focus out"
$(":text").each(function(index) {
$(this).focusout(function() {
var text = $(this).val();
text = $.trim(text);
$(this).val(text);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
First Name<br>
<input id="name" type="text" required><br>
Surname<br>
<input id="surname" type="text" required><br>
Address<br>
<input id="address" type="text" required><br>