Javascript regular expression for validation

Soldato
Joined
6 Nov 2005
Posts
8,066
Location
MK45
Can someone give me a point in the right direction with a javascript regular expression for validating a comment area for a contact form.

The basic alphanumeric one that came with the script I've got (mootools formcheck) is:

/^[a-z0-9 ._-]+$/i

But this is too strict, really - it won't allow most punctuation (not even commas)! Can someone give me something that will allow things like ? ! , : ; ( ) - but avoid things that aren't going to be needed in the field and pose risks, like $ _ < > [ ] { } etc?

Thanks for your help! I've been trying to figure this out for ages, but whenever I think I've got anywhere, I realise I've also let through a load of unwanted characters :(
 
That's what I tried, actually, but on testing, it was letting through < and >, as well as other bracket types. On looking about I saw that \ could be used to escape any functions the regular expression associates with particular characters, so I tried putting that in front of them, but that didn't work either.

I'll try putting that in when I get back home tonight to check I wasn't just doing something stupid, though. Thanks!

I have a working knowledge of most javascript (well, I can get by, anyway) and I'm much better with php, but these things really have me stumped :(
 
jQuery's excellent validation plug-in uses this for basic punctuation:

/^[a-z-.,()'\"\s]+$/i

Saves you lots of effort by using that plugin if you're already using jQuery.
 
I'm using mootools for all of my scripts, so I don't really want to introduce jQuery unless I have to. That looks like just the ticket though - I'll try it out later. I really should have thought to cannibalise that plug-in, but I kind of assumed that it would be the same as the one I'm using for mootools. I did spend quite a bit of time on google with searches like "validation for textarea" etc etc to no avail.

Cheers!
 
Hmm well it's kicking out stuff that I wouldn't expect with the regex: /^[a-z-.,()'\"\s]+$/i

I tested it with this, which as I understand it, should work:
Code:
This is a "test" of a 'test'! Something (has) to work? This, has some, commas.

It doesn't let that through, and removing any of the individual types of punctuation marks doesn't make any difference. Anyone have any ides :confused:?

The site is here, if anyone wants a play with the form to see what they can get through. It's still in dev, so most of it doesn't do that much just yet ...

http://www.gyphotography.co.uk/dev/v2/index.php?page=contact
 
Last edited:
try this.. it could be thinking the second hyphen is supposed to be specifying a range.

Code:
/^[a-z\-.,()'\"\s]+$/i

Any errors in the js console?
 
Ah I think I've got it from there. It's behaving as I'd expect now with the test string above and rejecting < { [ _ = etc.

Here's the final one I've got:
Code:
/^[a-z0-9 \-.,?!()'\"\s]+$/i
If anyone spots any errors with that looking over the thread, I'd appreciate it if you'd advise me accordingly :)
 
Last edited:
Back
Top Bottom