Javascript trim function example

Javascript trim function program and the screenshot of the output is given below,

Program:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
	function trim(withSpace) {
     	 return withSpace.replace(/^\s+|\s+$/g,"");
	}

	function getInput(){
		var input = document.getElementById("inp").value;
		document.getElementById("out").value = trim(input);
	}

</script>
<form>
Enter the input : <input type="text" id="inp"> <input type="button" onclick="getInput()" value="Trim"> <br>
output: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" id="out" >
</form>
</body>
</html>

 

Output:

trim

 

 

 

 

Thanks for reading this post………..!!!

Leave a Reply