Trim is used to remove white space from string. Trim is useful when we need to validate form and other database insertion of records. JavaScript directly don’t provide trim function to remove white spaces. We have to use string regular expressions to remove white space from string in JavaScript.
With regular expression we make own trim function in JavaScript and use as trim function in HTML form.
This example will work as trim function in JavaScript. We can use to call this trim function by passing string in trim function. Trim function return without white space string in JavaScript.
function trim(s) { return s.replace( /^\s*/, "" ).replace( /\s*$/, "" ); }
Use of Trim function
var fieldNameVariable = trim(document.formName.fieldName.value);



Link to Us