Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Validate fields

Quote Reply
Validate fields
Have someone an idea how to check entrees from users for fields:

^\d*\.?\d*$ ->should test on only numbers.

^[\w\d/_-]+$ ->wants only one word, only numbers, chars and _-

.+@.+\..+ -> wants something and @ and .

\d+ -> only numbers

So far i understand this, but i have no clue to make rules from this.


e.g i wanna have only chars (a-z,A-Z) and numbers, but no spaces before, between or at end.

Should be: ^\w\d*\.?\w\d*$



[This message has been edited by Robert (edited January 15, 2000).]
Quote Reply
Re: Validate fields In reply to
I could be too tired, but:

Code:
^[a-zA-z0-9]+$

The above makes sure you have at least one character, if you want to allow an empty set, you could use a '*' instead of the '+'

You couldn't use the \w because that would allow a '_' which wasn't in your selection set.

But if a '_' was leagal (as it is in Perl)

Code:
^\w+$

should do the same thing.
Quote Reply
Re: Validate fields In reply to
pugdog.. you're wrong about \w

\w is alphanumeric.. it doesn't allow '_'

it is equivalent to

[a-zA-Z0-9]

my validation for username is

^[a-zA-Z0-9-_]+$

jerry

[This message has been edited by widgetz (edited January 15, 2000).]
Quote Reply
Re: Validate fields In reply to
Jerry, I took that right out of the "Mastering Regex" book.... Whether '_' is included or not is somewhat system dependent.

If you don't want the '_' you are better defining the range without it.

Quote Reply
Re: Validate fields In reply to
I have all fixed without this one:

all numbers, all letters and space

I would use this for a additional searchfield in links. Someone have a clue?

Quote Reply
Re: Validate fields In reply to
Do you have an example of the pattern you want? Usually patterns like that are complex, not simple.
Quote Reply
Re: Validate fields In reply to
Hmmm,

i want to have a user insert something like this:

books shoe board 123

So i need a-z, A-Z, 0-9 and the space,
but e.g. no <>% and s on!
Quote Reply
Re: Validate fields In reply to
Hi Robert,

What about defining an allowed charset like:

my $ALLOWED_CHAR_SET='-a-zA-Z0-9 ';

Then kick-out all unwanted chars

$OK_STRING =~s/[^$ALLOWED_CHAR_SET]//g;
Quote Reply
Re: Validate fields In reply to
I hope to do this without any programming inside Links, cause (a) im no programmer, (sitting here for 5 hours only to do 3 scripts for my own affiliate with cookie-setting :-(
)and b) more changes in Links make more work for upgrading.

I believe there must be a solution for the validate fields inside Links and validate table.

I have a solution for my passord-field and some other things; the only one i fail till now is my keyword-field; there i want only chars and space.?
Quote Reply
Re: Validate fields In reply to
The character set is the way to go:

[a-zA-Z1-9 ]

The set is treated as a set of single characters, and the '-' is a meta character meaning "everything in between.

Quote Reply
Re: Validate fields In reply to
Some tips on regexp's:

^ - match at beginning.
$ - match at end.
\d - match any number 0 to 9.
\w - match any letter A - Z, lowercase a -z, any numbers 0-9 as well as the underscore _.
\s - match whitespace, normally only tab and spaces.
[] - character class, match anything inside the brackets.
* - match 0 or more occurrences.
+ - match 1 or more occurrences.
? - match 0 or 1 occurence.

From that list, you say you want to allow letters, numbers or spaces, but nothing weird like ^{& etc. You could then use:

^[\w\s]+$

if you wanted at least something to be filled in (note the plus for one or more).

or

^[\w\s]*$

if the field can be left blank (note the star meaning zero or more). Be sure to explain to your users what you are seeking, because this will cough up an error if they try to use any punctuation like apostrophe's, etc.

Also, note: accents are considered different in perl!! So a é is not the same as a e. If you want to allow accents, you have to put those into the character class like:

^[\w\dé]*$

Hope that helps,

Alex