Link to home
Start Free TrialLog in
Avatar of apresto
aprestoFlag 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
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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.



Avatar of apresto

ASKER

Thanks but which one of these would disable any spaces from being inserted?
I would add onchange event too, because user can use mouse or autofill form
Avatar of 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?
Did you test any of my upper proposals?
Avatar of 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
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of apresto

ASKER

Exactly what i was looking for, Thanks you
You are welcome.
Avatar of 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 can give you that, but if you realy allow ANYTHING but space, then you allow also %&$@#, and so on. Do you realy want that?
Avatar of 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
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?
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.
Avatar of 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
Avatar of apresto

ASKER

seeing as this is only a 250 question i will open another question for points for you for the great help