Closed Thread Icon

Topic awaiting preservation: How do i trim "space" input into a form field before validating (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=22974" title="Pages that link to Topic awaiting preservation: How do i trim &amp;quot;space&amp;quot; input into a form field before validating (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: How do i trim &quot;space&quot; input into a form field before validating <span class="small">(Page 1 of 1)</span>\

 
Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 08-18-2004 16:48

Hi guy,
I want o validate some html form fields with javascript and did something like this:

code:
if (form.field.value.length=="" ){
alert("Please Ensure that no Field is left blank.");
form.field.focus();
return (false);
}


........
fine it works but assuming the user presses the "space bar" as value for the field, my scripts assummes that ... form.field.value.lenght is not ==""... thus submiting the form.. i need to know how to trim the value before checking it. in JAVA i would have done something as aesy as String.valueof(field).trim()... but i am just new to javascript and i dont know of any way to trim my Strings... please help me!

kuckus
Paranoid (IV) Mad Librarian

From: Berlin (almost)
Insane since: Dec 2001

posted posted 08-18-2004 17:48

Javascript doesn't have a trim function built-in, but there are many available on the web - here's the first result of a Google search for "javascript trim function":

http://www.vermontsoftware.com/Javascript/trim.html

That should do the job just fine.

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-18-2004 18:02

I use this, personally:

code:
function stringTrim(strToTrim) {
return(strToTrim.replace(/^\s+|\s+$/g, ''));
}



Hope this helps,
Dan

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 08-19-2004 10:18

"To DAN" or who might ba able to explain...
Thanks "Dan" your function just works fine for me.. i think i am now getting realy into javascript too. i just want to ask one more thing.. can you explain how the combination of those characters does the "trim" Magic! i think it'll make me learn better if i understand how the fuction(s) i use work.

The trim function.....

code:
function stringTrim(strToTrim) {
return(strToTrim.replace(/^\s+|\s+$/g, ''));
}



Thanks,
Fikzy!

BillyRayPreachersSon
Nervous Wreck (II) Inmate

From: London
Insane since: Jul 2004

posted posted 08-19-2004 10:25
quote:
can you explain how the combination of those characters does the "trim" Magic!



Unfortunately, I can't - as RegExp is the one thing I've never learned properly.

I knocked that together about 1.5 years ago, with the aid of Google and about 30 mins of time... And was very surprised (and happy) when I found it worked!

Maybe someone else will be able to explain how my code works?

Dan

shingebis
Obsessive-Compulsive (I) Inmate

From: UK
Insane since: Aug 2004

posted posted 08-19-2004 11:59

Regular expressions are an incredibly useful thing to learn because once you've picked them up, they work the same way in pretty much any scripting language. (http://www.physics.rutgers.edu/~kotliar/perltut.html#77-BasicRegularExpressions is where I originally learned them...) There's far too much to condense into a post here, but a very brief explanation of how they work in this case:

The characters inside the / / make up a rule which specifies which characters to match, and the '' is what they get replaced with.

\s means match any whitespace - spaces, tabs, and line breaks. In general a \ in front of a letter signifies that the letter has a special meaning like this - if it wasn't there, we'd be stripping out the letter S instead of spaces.

+ means 'one or more of the previous character', so \s+ matches any amount of whitespace.

^ means 'the beginning of the string', and $ means 'the end of the string'.

| means 'or'.

The 'g' on the end is a modifier to tell it not to stop after finding the first match.

So, putting it all together, it says: 'If you find the beginning of the string followed by one or more whitespace characters, OR you find one or more whitespace characters followed by the end of the string, replace them with nothing, and carry on looking through the string for further matches'.

Fikzy
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2004

posted posted 08-19-2004 18:46

Thanks shingebis! your explanation was helpful and the site was usefull too. I think most programmer/Developers should get aquinted with "regexes"...

"Dan" you might wan to to look at the site site too.

http://www.physics.rutgers.edu/~kotliar/perltut.html#77-BasicRegularExpressions

It realy feels good to be able to explain how one's code!

Fikzy

« BackwardsOnwards »

Show Forum Drop Down Menu