Link to home
Start Free TrialLog in
Avatar of MichaelEvangelista
MichaelEvangelista

asked on

regEx to allow A-Z and single spaces only?

I am using a simple "onkeyup" with a regEx to replace anything but letters as the user types into a small text input. Works great... but I need to modify it so that *single* spaces are allowed.

Currently the code looks like this
<input type="text" size="12" maxlength="12" name="pfWord" id="pfWord"
onkeyup="this.value=this.value.replace(/[^a-z]/ig, '')">

How should I alter the regEx to also say
"allow spaces AND letter a-z"
AND
"replace more than one space with a single space"
?
Avatar of mr_egyptian
mr_egyptian
Flag of United States of America image

onkeyup="this.value=this.value.replace(/[^a-z][\s]{1,1}/ig, '')"
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 MichaelEvangelista
MichaelEvangelista

ASKER

PERFECT Zvonko, thank you!

Mr_Egyptian: this worked, except double spaces were removed rather than replaced with a single space.