The trim function does not work correctly
<input class="input"></input>
<div class="button">CLICK</div>
$(".button").click(function() {
var name = $( ".input" ).val();
name = $.trim(name);
console.log("TRIM " + name);
});
http://jsfiddle.net/5sufd9jj/
Trim removes whitespace from the beginning and end of a string.
If you want to remove consecutive spaces such as 'string string', use the following:
$.trim(name.replace(/\s+/g, ' '));
Updated Example
$(".button").on('click', function() {
var name = $.trim($('input').val().replace(/\s+/g, ' '));
console.log("TRIM " + name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="input"></input>
<div class="button">CLICK</div>
It is working all right.
trim function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string.
It DOES NOT remove spaces from the middle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With