0

Hello all,
I am trying to strip all of the white space,front and back, from a variable that has the value of a text box in it.
I've tried so many different techniques but to no avail...
here is an example of some of my code...

var firstname =(document.registration_form.FirstName.value);
    if (firstname == '') 
    {
        alert('Please fill in your first name!');
        return false;
    }

Just to repeat myself, how can I strip all of the spaces in the above variable "firstname"?

Thanks in advance!

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by ivatanako
0

if you are only reffering to a white space, you can remove it by

$name = str_replace(' ','',$name);
echo $name;
//syntax str_replace('the string to be replace','replace with this string',$the variable)

hope this help

0

Or if you're referring to javascript.

var firstname =(document.registration_form.FirstName.value.replace(" ",""));    
if (firstname == '')     
{        
alert('Please fill in your first name!');        
return false;    
}
0

I tried this and it didn't work for me.

Any suggestions about how to proceed?

0
function white_space(field)
{
     field.value = (field.value).replace(' ','');
}

//example HTML
<form action="" method="get" name="test_form">
<input type="text"   name="firstname"   value="Juan De La Cruz"/>
<input type="button" name="button" value="button"  onclick="white_space(firstname)"/>
</form>
0

Maybe posting the whole code will help.

Just tried my sample, and it worked. :)

<html>
<head>
<script type="text/javascript">
function test()
{
var firstname =(document.registration_form.FirstName.value.replace(" ",""));    
if (firstname == '')     
{        
	alert('Please fill in your first name!');        
	return false;    
}
}
</script>
</head>
<body>
<form name="registration_form" id="registration_form" onsubmit="test();return false">
<input type="textbox" name="FirstName" id="FirstName" />
<input type="submit" />
</form>
</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.