Trim Strings

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2005
Posts: 188
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Trim Strings

 
0
  #1
Nov 6th, 2006
How can I trim a string i.e. remove leading and trailing spaces ? I have a form validation which requires the user to specify a value, i.e. not leave the field blank. I want to ensure that the user cannot simply press the Spacebar a few times and leave the field.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Trim Strings

 
0
  #2
Nov 6th, 2006
as an evil one-liner:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input onBlur="alert(this.value.match(/[\w\d]/)!=null?'OK':'NOT OK');">

or more useably:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function check_field(ident){
  2. var target = document.getElementById(ident);
  3. return target.value.match(/[\w\d]/) != null;
  4. }
  5. function check_form(){
  6. if(check_field('the_one_that_can't_be_spaces')){
  7. //Everything's fine!
  8. }else{
  9. //Something's missing o_O
  10. }
  11. }

if the String.match(/regex/) function returns a valid match for \w (any alphabetic character) or \d (any number) (notation [\w\d]) then theres nothing valid in the field.. change the regex to change the matching cases.

Some reading may be helpful:
http://www.webreference.com/js/column5/
(^ this ones a bit lame to be honest, check out the PERL regex tutorials, the syntax once your in an expression is identical: http://www.perl.com/pub/a/2000/11/begperl3.html)

if you want to trim the string... you can do a String.split(/[ ]{1,}/) (returns a String array); but if you don't know what's been entered you may need alot of logic to work out where your (valid) values are... if you split by an indefinate number of spaces ([ ]{1,}) then if the string is all spaces; the length of the resulting array (should) be 0...
Last edited by MattEvans; Nov 6th, 2006 at 2:52 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 232
Reputation: Rhyan is an unknown quantity at this point 
Solved Threads: 24
Rhyan's Avatar
Rhyan Rhyan is offline Offline
Posting Whiz in Training

Re: Trim Strings

 
0
  #3
Nov 7th, 2006
Javascript is needed if you want your code to be validated before form submission. If you are going to validate after submit, you have to check your server-side language manual for the function that can check this for you. For example if you're using PHP, you can do something like this

$sumbitted_value=$_POST['field_name_that_should_not_be_blank'];
if (empty($submitted_value) or trim($submitted_value)==NULL)
{
echo 'Something's missing';
}

In this way we both check if the value is not empty, or when removed spaces from both ends the value is not null.
If you want to check if the value is numeric or char, it is completely different story.

What is not good with my method is that your users will have to go back to the previous page to change or enter the missing values.

Check the validation scripts that Dreamweaver offers, if you're familiar with Dreamweaver. It can check both - whether values exist and if the type of values correspond to the requirements. If error occures, an alert box will be showed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 188
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Trim Strings

 
0
  #4
Nov 7th, 2006
Thanks to both of you. MattEvan's solution should be what I am looking for (validating from the client side). I am using ASP for the server side coding, where such validations are a breeze :-).

I am rather new to Javascript, have been working mostly in VB. Coming from such a VB background, I find it amazing that doing something simple like trimming a string takes so much effort, inspite of there being a inbuilt String class. Well.... I guess I'll just have to get used to it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 3592 | Replies: 3
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC