Gossamer Forum
Quote Reply
Regex Help
I've looked all through the forum and also tried altering some of the existing Form Regex in Links and I just can't figure out how to write a regex that prevents special characters in the Title field.

What I would like is to disallow punctuation marks like "!#&$'- from the Title column. I have a javascript that prints the Titles, and anytime someone has used a punctuation in a Title it breaks the javascript.

If someone could explain the logic to me, that would be a big help. I'm looking for a regex that can find a "-" anywhere in the Title and reject it.

This Regex:
Code:
^(https?:\/\/|ftp:\/\/|news:\/\/|mailto:)

looks for those particular patterns, but how would I change it to look for a - ?

Thanks,

Bryan

Last edited by:

BryanL: Feb 19, 2002, 9:49 AM
Quote Reply
Re: [BryanL] Regex Help In reply to
In the table definition editor in the admin panel, click on the Title column and for the regex enter something like:

^\w+$


That allows a-z, 0-9 and _
Quote Reply
Re: [RedRum] Regex Help In reply to
Thanks, that got me on the right track.

What I ended up using is this:

^[a-zA-Z0-9_\s\,]+$

I need to allow Titles that had spaces or commas in it. Probably not the most elegant way to do it, but hey, it works!

I also found this helpful site to explain the basic functions:

http://www.codebits.com/p5be/ch10.cfm

Thanks!


Bryan
Quote Reply
Re: [BryanL] Regex Help In reply to
Another option would be to add a global:

javascript_title =>

sub {
my $tags = shift;
my $title = $tags->{Title};
$title =~ s{([\\\/'"])}{\\\$1}g;
$title =~ s{(?:\r\n|\r|\n)}{\\\n}g;
return $title;
}

The return will be safe inside single quotes in javascript. Then just put <%javascript_title%> instead of <%Title%> in your javascript.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [BryanL] Regex Help In reply to
>>^[a-zA-Z0-9_\s\,]+$<<

\w can be used instead of a-zA-Z0-9 and _ ....so:

^[\w\s\,]+$

Last edited by:

RedRum: Feb 19, 2002, 1:59 PM
Quote Reply
Re: [RedRum] Regex Help In reply to
Yup, that works: ^[\w\s\,]+$

I thought I read somewhere though, that \w would not work within brackets, or it would change its meaning. Hmm.. guess it's OK.

Alex's idea is good too, but I'm going to add this Regex to the Logo_Graphic column because that was also causing an error. I don't think commenting it out will work for that.

It's amazing what people will name files, one guy used an "!" in the middle of his filename, go figure!

Bryan