• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 933
  • Last Modified:

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"
?
0
MichaelEvangelista
Asked:
MichaelEvangelista
1 Solution
 
mr_egyptianCommented:
onkeyup="this.value=this.value.replace(/[^a-z][\s]{1,1}/ig, '')"
0
 
ZvonkoSystems architectCommented:
Check this:





<input type="text" size="12" maxlength="12" name="pfWord" id="pfWord"
onkeyup="if(!this.value.match(/^([a-z]+\s?)*$/i))this.value=this.value.replace(/[^a-z ]/ig,'').replace(/\s+/g,' ')" >



0
 
MichaelEvangelistaAuthor Commented:
PERFECT Zvonko, thank you!

Mr_Egyptian: this worked, except double spaces were removed rather than replaced with a single space.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now