Thats Fab!!! Thank you so much! Works a treat.
If I was to restrict more than one character of my choice would I just add it within the ' ' or is there another way to do it?
Many thanks for your time
Dan
Main Topics
Browse All TopicsHello
I need to stop people entering spaces in a text field so is it possible to restrict them from doing this? Also I may need at a later date other characters restricted within another field.
Could you please show me how to do this if possible?
Many thanks
Dan
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Two ways for you:
<html>
<head><title>Demo</title>
</head>
<body>
<form>
<p>Remove after input complete: <input type="textbox" onchange="this.value = this.value.replace(/ /g,'');" ><br />
(Note the / /g contains a space, and it is a regular expression, and so has no quotes - see the RegExp object for details. This will allow you to block other characters/patterns).
</p>
<p>Remove during input: <input type="textbox" id='one' ><br />
<script>
// Get out input field
var input = document.getElementById('o
//attach a function to return false if the key pressed is a space
input.onkeypress = function ( e ) {
if(!e) var e = window.event;
if(e.keyCode){
key = e.keyCode;
} else if(e.which){
key = e.which;
}
return ( String.fromCharCode( key ) != " ")
}
</script>
</p>
</body>
</html>
A modification of cem_turk's answer:
If you would like to restrict multiple characters, pass thru an array of characters and parse the list like this:
<script type="text/javascript">
function restrict(tbox,chr)
{
for(var i=0; i<=chr.length; ++i)
tbox.value=tbox.value.repl
}
</script>
Space, dots and the letter 'g' are not allowed here: <input type="text" onkeyup="restrict(this,[' ','.','g'])">
Business Accounts
Answer for Membership
by: cem_turkPosted on 2006-02-28 at 04:30:43ID: 16064594
<script type="text/javascript"> ace(chr,'' ); //replaces specified character with nothing
)">
function restrict(tbox,chr) {
tbox.value=tbox.value.repl
}
</script>
Spaces are not allowed here: <input type="text" onkeyup="restrict(this,' ')"><br>
Dots are not allowed here: <input type="text" onkeyup="restrict(this,'.'
when calling restrict function specify the character to be restricted inside the aposthropes ' ' for space '.' for dot etc...