Avatar of apresto
apresto
Flag for Italy asked on

OnKeyDown Alert - Want to disable spacebar

Hey guys, simple question probably, i am working on a user registration form and i want their UserName to be spaceless (no spaces), is there a way to put a little OnKeyDown procedure in the username Textfield that will either alert the user when they atempt to hit space or will just not let them hit space

Example:

This one alerts when the user attempts to hit anything on the keyboard that isnt a number:

<input type="text" name="numOnly" size="10" onkeypress="if(isNaN(String.fromCharCode(event.keyCode))){alert('Please only enter numbers.');return false;}">

?

Thanks guys
JavaScript

Avatar of undefined
Last Comment
apresto

8/22/2022 - Mon
Zvonko

Check this for numbers:

<input type="text" name="numOnly" size="10" onKeyUp="if(this.value.match(/\D/)this.value=this.value.replace(/\D/g,'')">


And this for names:

<input type="text" name="numOnly" size="10" onKeyUp="if(this.value.match(/\W/)this.value=this.value.replace(/\W/g,'')">

Only problem with the meta character \W is that it would allow names like this one:  11111111

So if you like to have only characters you have to use this:

<input type="text" name="numOnly" size="10" onKeyUp="if(this.value.match(/[^a-z]/i)this.value=this.value.replace(/[^a-z]/gi,'')">

If you like to add more acceptabe characters to the set, then add them to the square braces set:

<input type="text" name="numOnly" size="10" onKeyUp="if(this.value.match(/[^a-z\-\_]/i)this.value=this.value.replace(/[^a-z\-\_]/gi,'')">

The upper expression does add the dash and the underscore character to allowed character set.



apresto

ASKER
Thanks but which one of these would disable any spaces from being inserted?
devic

I would add onchange event too, because user can use mouse or autofill form
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
apresto

ASKER
ok, maybe im not clear sorry about that

i dont know much javascript, i just need to know how to stop the user from entering aspace into the textbox - is this possible, if its not possible to sto this initially is it possible to alert them on formsubmit so they can go back and change?
Zvonko

Did you test any of my upper proposals?
apresto

ASKER
no im not sure which one it is

i didnt write the example i gave above about only numbers being allows into the text field i got it from another question and it doesnt relate to what i need to do i just wanted a similar feature where i can put some javasript actually in the Input tag using the keypress event

this is what the textbox looks like tight now:

<input type="username" type="text" maxlength="16">

i was hoping to just add something to that
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Zvonko

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
apresto

ASKER
Exactly what i was looking for, Thanks you
Zvonko

You are welcome.
apresto

ASKER
Hi Zvonko, i know i closed the question but i just realised that this code you gave me that i accepted doesnt allow me to enter numbers - couldyou possibly amend this so that it allows everything BUT a blank space (space bar)?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Zvonko

I can give you that, but if you realy allow ANYTHING but space, then you allow also %&$@#, and so on. Do you realy want that?
apresto

ASKER
ah i see thats a good point!

is there a way to disallow space and those characters?

i can open another question if you like - let me know if its "new question worthy" and ill open another one, its not a problem

thanks - Good call
Zvonko

Simply say what characters you want to allow and I can make it here.
For example, do you want this characters to be allowed:  a-z0-9
Any else character?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Zvonko

Also perhaps you want to allow that first character is alpha, and second and following can be alpha or digit
You can also check for at least three characters value length.
apresto

ASKER
wow thats more than i ever hoped for if you could create something that:

allows only
- a-z
- A-Z
- 0 - 9

does not allow <spacebbar>

has to begin with letter

and must be atleast 3 digits that would be fantastic
apresto

ASKER
seeing as this is only a 250 question i will open another question for points for you for the great help
Your help has saved me hundreds of hours of internet surfing.
fblack61