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!

Recommended Answers

All 5 Replies

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

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

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

Any suggestions about how to proceed?

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>

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