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.

as an evil one-liner:

<input onBlur="alert(this.value.match(/[\w\d]/)!=null?'OK':'NOT OK');">

or more useably:

function check_field(ident){
  var target = document.getElementById(ident);
  return target.value.match(/[\w\d]/) != null;
}
function check_form(){
   if(check_field('the_one_that_can't_be_spaces')){
     //Everything's fine!
   }else{
     //Something's missing o_O
   }
}

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...

Member Avatar for Rhyan

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;
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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.