homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Form input verify: accept letters, spaces and list of chars only.

 3:10 pm on Feb 16, 2006 (gmt 0)

I want to verify a string and make sure it contains the following characters only:

1. Letter A~Z, a~z
2. Space
3. - character
4. ( and )

Question 2 - What if I only want to accept letters:

1. Letter A-Z, a-z only

Mr. RegEx help please.


Rambo Tribble

 4:17 pm on Feb 16, 2006 (gmt 0)

I believe Mr. Marx is the resident King of the Regular Expression. I'm afraid all my expressions are irregulars, overruns, and cosmetic seconds (they are, however, available at stunning discounts). Yes, Mr. Marx is who you need (sorry to string you along).

All apologies, I just couldn't help myself. Please consider this a gratuitous bump.

[edited by: Rambo_Tribble at 4:23 pm (utc) on Feb. 16, 2006]


 4:21 pm on Feb 16, 2006 (gmt 0)

To prevent spam injection attacks, and issues caused by people switching off Javascript on their clients, it's also worth revalidating at the server side.


 6:03 pm on Feb 16, 2006 (gmt 0)

1. Letter A~Z, a~z
2. Space
3. - character
4. ( and )

newstring = string.replace(/[^a-z \(\)-]+/gi, '');

Only letters A~Z, a~z

newstring = string.replace(/[^a-z]+/gi, '');


 6:04 pm on Feb 16, 2006 (gmt 0)

What about letters like ? ;)

Bernard Marx

 10:34 pm on Feb 16, 2006 (gmt 0)

Precis som min tnkande, Doc.

Back in Blighty, when studying or coding, it's tempting to take the attitude: "I'll think about that later", or "Well, it's their own silly fault for being so foreign". In Stockholm, of course, at work or in the classroom, these characters just won't go away.

Putting a customer input form on the Web is like that, but worse. I still can't think of a generalised solution.



Javascript (Rhino) thinks is a word character.
JScript disagrees.

I was thinking of proposing:

char.toLowerCase()!= char.toUpperCase();

but that would lose characters that have no upper¦lower case defined, like

The King is in the altogether, the altogether,..

Rambo Tribble

 11:31 pm on Feb 16, 2006 (gmt 0)

Regis Maximus (or is that Marximus?), do Perl's RegEx's offer relief?

Bernard Marx

 11:58 pm on Feb 16, 2006 (gmt 0)

Not sure. I tried it once and ran away. I'd guess Perl has some kind of what's-it-called collation (..?)

In Javascript "" > ""
..but not in Jmtland.

With the right collation, Java can avoid getting people lost in Swedish dictionaries, but not Javascript it seems.

Bernard Marx

 12:06 am on Feb 17, 2006 (gmt 0)


Regis Maximus

Do what? There's some serious misdeclension going on there, Tribble.

Regis : genitive
Maximus: nominative

I believe you were after the vocative:

Rex Maxime

(Now go back and do it again 100 times, etc...)

Rambo Tribble

 2:38 am on Feb 17, 2006 (gmt 0)

So, you're saying you have more in common with a dead lizard than an obnoxious talk show host? And wouldn't the nominative be appropriate for the titular?

Anyway, with server-side languages having more powerful regular expression tools, isn't this a job for AJAX-man? (cue the music and pan to the phone booth)


 3:56 am on Feb 17, 2006 (gmt 0)

You can always use a call-back function to determine which characters to let through.

<script type="text/javascript">
function strtest(string) {
alert('Before\n' + string);
string = eval("string.replace(/([^a-z])/gi, function(chr) { charCode = chr.charCodeAt(0); if(charCode < 192 ¦¦ charCode > 255 ¦¦ charCode == 215 ¦¦ charCode == 247) { return ''; } else return chr; })");
alert('After\n' + string);

That should let all letters in ISO-8859-1 pass :)

Rambo Tribble

 5:33 am on Feb 17, 2006 (gmt 0)

I would think at some point its going to get easier to test for the characters you wish to exclude. (And, no, Mr. Marx, I do not mean the likes of ourselves.)


 5:38 am on Feb 17, 2006 (gmt 0)

Ack! Don't ever say that ;)
It is never wise to attempt to "exclude" characters you are "afraid of" ...
Guaranteed, you will miss at least one, and someone will find it and wreak havoc (possibly even cause malicious stuff to happen).

Always disallow everything but what you want/allow.


 7:15 am on Feb 17, 2006 (gmt 0)

Hi guys, please check my final codes:

Q1. if(/[^A-Z a-z \(\)-]+/.test(v)){ alert('invalid input');}

Q2. if(/[^A-Za-z]+/.test(v)){ alert('invalid input');}

New question: What's the meaning of letter g in this regex: (/[^A-Za-z]+/g.test and what's about (/[^A-Za-z]+/gi.test?

Bernard Marx

 10:28 am on Feb 17, 2006 (gmt 0)


g - global

This would be needed (for instance) if you were doing a replace, else it would stop after first replacement:

"ABBA".replace(/B/,"X") // -> AXBA
"ABBA".replace(/B/g,"X") // -> AXXA

i - ignore case

"ABBA".replace(/b/g,"X") // -> ABBA
"ABBA".replace(/b/gi,"X") // -> AXXA

>> Q1. /[^A-Z a-z \(\)-]+/

Nothing wrong with it. It works. You could save a few chars..

- The + is unnecessary here
- It has two spaces
- Use i

/[^A-Z \(\)-]+/i

>> Q2. /[^A-Za-z]+/

Fine. Same things apply..


R. Tribble: And wouldn't the nominative be appropriate for the titular?

Since you were addressing Regem Maximum, I would say that that vocative is our man.

Doc, I had an idea concerning your latest
..but I'd better do some work first.

Rambo Tribble

 1:53 pm on Feb 17, 2006 (gmt 0)

"Since you were addressing Regem Maximum . . ."

I see your point. But, on a technical note, since this is the electronic medium, shouldn't the e-vocative be used? Granted, I tend more toward the provocative, myself.

DrDoc, I appreciate what you're saying and I have heard it before. I'm just not sure I entirely buy into it for every case. Efficiency has to count for something.

A thought occurs, though I don't have time to investigate it, at the moment; I wonder if URL-encoding of the string could offer any relief for testing purposes?

Bernard Marx

 4:02 pm on Feb 17, 2006 (gmt 0)

I think DrDoc has something (although I do quite vehemently eschew his use of

I really don't know much about character encoding etc, so I'm accepting his figures (and wisdom). I think there is a way to perform the same logic as his function, but with a straight replace(reg.""). This would be done using ranges of unicode escapes [\uMMMM-\uNNNN].

I started on it, then I realised that I have some ASP soup to unravel, which will take a while, since I added the noodles myself.

But, on a technical note, since this is the electronic medium, shouldn't the e-vocative be used?

I decline to take this matter any further.

Bernard Marx

 4:18 pm on Feb 17, 2006 (gmt 0)

Just spotted: DrDoc's outright dismissal of vehemence

Accessibility > Cognitive

Rambo Tribble

 11:36 pm on Feb 17, 2006 (gmt 0)

Let me vociferously denounce the detraction of 'vehemence'. (After all, if we are all using the K Desktop Environment, like Uncle Linus says we should, our little dictionary window can be close at hand, and quite accessible.)

To refrain from using the tools our language provides is to make the richness of that language unaccessible. Should all education and learning stop at the fifth grade? Vehemence is about passion; if we emasculate our verbiage, what passion can it hold? How usable is a language bereft of the many colors and textures of meaning contained in a broader and deeper lexicon?


 12:58 am on Feb 18, 2006 (gmt 0)

But if your nifty content contains nothing but such words, you bet it will be difficult to gain much traffic from search engines ;)

Rambo Tribble

 2:31 am on Feb 18, 2006 (gmt 0)

Perhaps, though a search on Google for 'vehement' produces 1,820,000 hits. Admittedly, it's easier to stand out with 'dumb-down', at 313,000. 'Vehement eschewal' renders 312. 'Eschew', recently used in a headline by The Register, rates 1,630,000.

Rambo Tribble

 3:23 pm on Feb 20, 2006 (gmt 0)

Apropos: "Literacy Limps Into the Kill Zone" [wired.com]. Et tu, DrDoc?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved