pTutorial

New way to learn Programming

JavaScript Remove Space From String

how to remove spaces javascript

In this article we demonstrate how to remove Spaces from the string (leading and trailling spaces) by using JavaScrip and Jquery with the help of live example. The following JQuery Script can be used to remove spaces from the sequence of character.

Remove white spaces example

<!DOCTYPE html>
<html>
<head>
<link href="http://freeonlinecompiler.ptutorial.com/
css/bootstrap.min.css" rel="stylesheet">
<script src="http://freeonlinecompiler.ptutorial.com/
js/jquery.min.js"></script>
<script>

$(document).ready(function (e) {
$('#kk').click(function () {
var z = $('#uk').val();
if(z==''){
$('#uk').css("border-color","red");
alert("Please enter first");
}else{
var z1 = z.replace(/(^\s+|\s+$)/g,'');
$("#divData").text(z1);
$('#uk').css("border-color","green");
return false;

	}
})
});
</script>

</head>	
<body>
<div class="container-fluid">
<div class="col-sm-4"></div>
<div class="col-sm-4">
<h4>Remove leading spaces and trailing spaces 
</h4>
<div class="form-group">
<label>Enter Name *</label>
<input class="form-control" type="text" id="uk" 
name="kk1">
</div>
<input type="submit" id="kk" class="btn btn-danger" 
value="Check">
<div id="divData"></div>
</div>
<div class="col-sm-4"></div>
</div>

</body>
</html>

Download

See Also


QUOTES OF THE DAY

Tomorrow life is too late, live today.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER