1. Remove all spaces before and after the string:
The code is as follows:
Function Trim (str)
{
Return str. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
Note:
If jQuery is used to directly use the $. trim (str) method, str indicates to remove all leading and trailing spaces.
2. Remove all spaces in the string (including spaces in the middle). Set the 2nd parameter to g)
The code is as follows:
Function Trim (str, is_global)
{
Var result;
Result = str. replace (/(^ \ s +) | (\ s + $)/g ,"");
If (is_global.toLowerCase () = "g ")
{
Result = result. replace (/\ s/g ,"");
}
Return result;
}
3. jquery example
Function bt_submit (){
Var $ title = $. trim ($ ("# title"). val ());
If ($ title = ''){
ShowMessage ("the carrier name cannot be blank", "# ff0000 ");
Return;
}
Document. all. form1.submit ();
}
The html code is as follows:
<Tr>
<Td width = "18%" align = "right"> carrier name: </td>
<Td colspan = "3"> <s: textfield id = "title" key = "operatorbean. title "size =" 88 "value =" % {# request. operatorbean. title} "/> </td>
</Tr>