»
EnglishFrenchVietnamese

Print - Remove_XS_Whitespace - JavaScriptBank.com

Full version: jsB@nk » Utility » Remove_XS_Whitespace
URL: https://www.javascriptbank.com/remove-xs-whitespace-script.html

Remove_XS_Whitespace © JavaScriptBank.comStrips excess spaces from a field. Users sometimes enter " " in required fields to trick most validation that only checks for "" length fields. This JavaScript takes a string and removes the leading and trailing spaces and also any extra spaces in the string. IE only.

Full version: jsB@nk » Utility » Remove_XS_Whitespace
URL: https://www.javascriptbank.com/remove-xs-whitespace-script.html



JavaScript
<SCRIPT LANGUAGE="JavaScript"><!-- Begin// by Pat Cahill , patrickcahill@bigfoot.com , http://www.pcap.org.au/pat/fields.htmlfunction remove_XS_whitespace(item){  var tmp = "";  var item_length = item.value.length;  var item_length_minus_1 = item.value.length - 1;  for (index = 0; index < item_length; index++)  {    if (item.value.charAt(index) != ' ')    {      tmp += item.value.charAt(index);    }    else    {      if (tmp.length > 0)      {        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1)        {          tmp += item.value.charAt(index);        }      }    }  }  item.value = tmp;}//  End --></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<BODY onLoad="mystring.select()"><B>String to strip excess spaces from:</B><br><INPUT TYPE="text" NAME="mystring" VALUE="  This  is  a  string  " SIZE="50" MAXLENGTH="50"><BR><INPUT TYPE="SUBMIT" NAME="submit" VALUE="Strip XS Spaces" ONCLICK="remove_XS_whitespace(mystring);mystring.select();return false"><BR><BR>Try <A HREF="" ONCLICK="mystring.value='        ';mystring.select();return false;">8 blank spaces</A><BR>or <A HREF="" ONCLICK="mystring.value='        This    is      a    string        ';mystring.select();return false;">try this string</A><BR>or <A HREF="" ONCLICK="mystring.value='          1234567890          1234567890          ';mystring.select();return false;">try this string</A></body><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->