Welcome to WebmasterWorld Guest from 54.145.44.134

Forum Moderators: open

Message Too Old, No Replies

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

     

iProgram

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

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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]

dragonthoughts

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

5+ Year Member



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.

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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, '');

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



What about letters like ? ;)

Bernard Marx

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

WebmasterWorld Senior Member 10+ Year Member



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.

Try:

alert(/\w/.test(""))

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)

WebmasterWorld Senior Member 10+ Year Member



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

Bernard Marx

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

WebmasterWorld Senior Member 10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



..and.

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)

WebmasterWorld Senior Member 10+ Year Member



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)

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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);
}
strtest("!te@st#in%g");
strtest("@#$*");
</script>

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

Rambo Tribble

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

WebmasterWorld Senior Member 10+ Year Member



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.)

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

iProgram

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

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



Modifiers:

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..

/[^A-Z]/i



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)

WebmasterWorld Senior Member 10+ Year Member




"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)

WebmasterWorld Senior Member 10+ Year Member



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

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)

WebmasterWorld Senior Member 10+ Year Member



Just spotted: DrDoc's outright dismissal of vehemence

[webmasterworld.com...]
Accessibility > Cognitive

Rambo Tribble

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

WebmasterWorld Senior Member 10+ Year Member



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?

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month