
March 8th, 2005, 09:00 AM
|
 |
Alter Ego Wizard
|
|
Join Date: Jun 2004
Location: Edinburg Tx
|
|
|
Javascript Trim function
Seeing how most of the javascript validation that I've seen so far with textboxes check for something like document.forms[0].somefield.value == ""
It is not really proper checking for empty text field as a user may type is spaces, thus making this validation useless.
Here is a little Javascript function that works just like VBScript's Trim Function
Code:
function Trim(s)
{
// Remove leading spaces and carriage returns
while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
{ s = s.substring(1,s.length); }
// Remove trailing spaces and carriage returns
while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
{ s = s.substring(0,s.length-1); }
return s;
}
|