Tom Pimienta

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromTom Pimienta
ToMe
Subjectform validator script should trim leading/trailing spaces in text fields
Date1 December 2004 4:24
Wow, what an awesome site.  I looked at some of your demos, very nice. 

Just a suggestion:
The form validator should consider that the user might enter leading
and/or trailing spaces, eg: [space][space]Tom[space], in a text field. 
The validation function should trim spaces and evaluate what is left. 
This is easy to add and I'm sure you'll come up with a better method
than mine:

function strip_spaces( string )
{
    //brute force
    while( string.charAt( 0 ) == " " )
    {
        string = string.substr( 1, string.length - 1 );
    }
    while( string.charAt( string.length - 1 ) == " " )
    {
        string = string.substr( 0, string.length - 2 );
    }
    return string;
}

Once again, you site is amazingly awesome stuff; my favorite was
"draggable layer".

-Tom
FromMe
ToTom Pimienta
SubjectRe: form validator script should trim leading/trailing spaces in text fields
Date3 December 2004 23:15
Tom,

> Wow, what an awesome site.

thankyou :)

> The form validator should consider that the user might enter leading
> and/or trailing spaces, eg: [space][space]Tom[space], in a text field.

Well, maybe yes and maybe no. some people may actually not want to allow
people to add leading or trailing space, as depending on the purpose of the
imput, that could invalidate the data. Personally, if I wanted to ignore
leading/trailing whitespace, I would remove it immediately after they write
it. After all, when all is said and done, you don't really want that
whitespace sent back to the server anyway.

This is what I would use:
<input ... onblur="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'');">
(the regular expression is basically the equivalent of your function, but
it would also take tabs, line breaks, form feeds, etc. into account)

But you could always change the regular expressions to allow whitespace:
/\s*[a-z]{2,}(-[a-z]{2,}){0,2}\s*/i
/\s*[\w\-\+]+(\.[\w\-\+]+)*@([\w\-áàäçéèêñóòôöüæøå]+\.)+[a-z]+\s*/i


An easy change, but one I would prefer to leave upto the person who is
using the script on their site.

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.