Gossamer Forum
Home : Products : DBMan : Customization :

Regular expression - digits only...

Quote Reply
Regular expression - digits only...
Hello!

I could not find the right expression for the .cfg file (definning database fields).
i've tried \d [0-9] etc ... but there is problem with spaces, other chars...

I have a field that stores ICQ numbers.. So what regular expression should i use to check that the entered value is at least 5 digits and there are no spaces(in the middle, trailing), and no other characters...?


[This message has been edited by db (edited April 26, 2000).]
Quote Reply
Re: Regular expression - digits only... In reply to
Oliver, another DBMAN user, is using the following valid expression for his ICQ field: '\d{1}'.

------------------
Eliot Lee....
Former Handle: Eliot
Anthro TECH, L.L.C
anthrotech.com
* Check Resource Center
* Search Forums
* Thinking out of the box (codes) is not only fun, but effective.


Quote Reply
Re: Regular expression - digits only... In reply to
Unfortunally, this \d{1} does not work for me. If a user has spaces or other chars between(at the end/beginning) his/her ICQ UIN, DBMan still accepts the input.
Quote Reply
Re: Regular expression - digits only... In reply to
The best way to do this is to change sub validate_record in the db.cgi file.

Change

Code:
foreach $col (@db_cols) {
if ($in{$col} =~ /^\s*$/) { # entry is null or only whitespace
($db_not_null{$col}) and # entry is not allowed to be null.
push(@input_err, "$col (Can not be left blank)"); # so let's add it as an error
}
else { # else entry is not null.
($db_valid_types{$col} && !($in{$col} =~ /$db_valid_types{$col}/)) and
push(@input_err, "$col (Invalid format)"); # but has failed validation.
}
(length($in{$col}) > $db_lengths{$col}) and
push (@input_err, "$col (Too long. Max length: $db_lengths{$col})");
if ($db_sort{$col} eq "date") {
push (@input_err, "$col (Invalid date format)") unless &date_to_unix($in{$col});
}
}

to

Code:
foreach $col (@db_cols) {
if ($in{$col} =~ /^\s*$/) { # entry is null or only whitespace
($db_not_null{$col}) and # entry is not allowed to be null.
push(@input_err, "$col (Can not be left blank)"); # so let's add it as an error
}
else { # else entry is not null.
($db_valid_types{$col} && !($in{$col} =~ /$db_valid_types{$col}/)) and
push(@input_err, "$col (Invalid format)"); # but has failed validation.
(length($in{$col}) > $db_lengths{$col}) and
push (@input_err, "$col (Too long. Max length: $db_lengths{$col})");
if ($db_sort{$col} eq "date") {
push (@input_err, "$col (Invalid date format)") unless &date_to_unix($in{$col});
}
if ($db_sort{$col} eq "numer") {
if ($in{$col} =~ /a-zA-Z_|\W/) {
push (@input_err, "$col (Invalid number)")
}
}
}
}

Also, in your .cfg file, to make sure there are at least 5 digits, use

Code:
'\d{5}'

Make sure that you set the field_type to 'numer'.


------------------
JPD






Quote Reply
Re: [JPDeni] Regular expression - digits only... In reply to
Just a slight ammendment to JPDeni's routine...

If you have the Censoring routine (or other routines) as part of sub validate_record, do not close the for-each or else loops of JPDeni's code i.e. omit the last two braces '}'
Quote Reply
Re: [JPDeni] Regular expression - digits only... In reply to
I want to allow numbers AND spaces (but nothing else). Can I simply use:

Code:

if ($in{$col} =~ /a-zA-Z_|\W/x) {
push (@input_err, "$col (Invalid - numbers and spaces only)")
}


It doesn't work though... Also tried:
if ($in{$col} !~ /[0-9] /){ push (@input_err, "$col (Invalid - numbers and spaces only)") }

if ($in{$col} !~ /[0-9]\s/){ push (@input_err, "$col (Invalid - numbers and spaces only)") }

but can't get it to match up.

Any help gratefully received.