Gossamer Forum
Home : Products : DBMan : Customization :

Reg expression Syntax help pls

Quote Reply
Reg expression Syntax help pls
Afternnon.
I want a regexp in the cfg that allows 0.. numeric and/or up to 2 dec places e.g 37.5 or 37.50 or 37
I have used [\d+]{0,4}\.{0,2}[\d]$'] but that is not working, the maxlength is 10 so should be ok?
Any clues?


Quote Reply
Re: Reg expression Syntax help pls In reply to
Code:
/\d(\d?)\.\d(\d?)|\d/;
I think that will allow 1.1, 11.1, 1.11, 11.11 or 0-9 (I THINK) etc....
Im clutching at straws. :(



Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Reg expression Syntax help pls In reply to
I have this currently in the cfg in the vaild_expr column:
'^[\d+]{0,4}\.{0,2}[\d]$'], which is ok but it allows input of 37..5 (two points).

I have tried your syntax but I get fatal errors :(

Quote Reply
Re: Reg expression Syntax help pls In reply to
Sorry - I should check before I post

Paul Wilson.
http://www.wiredon.net/gt/
http://www.perlmad.com/
Quote Reply
Re: Reg expression Syntax help pls In reply to
Hey it's no prob, thanks for the help Smile

Quote Reply
Re: Reg expression Syntax help pls In reply to
Ok so I've reverted to using hard coding in the sub validate mod, thus:
($in{'test'} < 0 or $in{'test'} > 999.99 or $in{'test'} == /\d..*/)

I just can't seem to stop it allowing two decimal points (typo) after the value e.g. 110..64. :(

Quote Reply
Re: Reg expression Syntax help pls In reply to
This may not even be close to what you want, but it helped me when I needed 3 decimal places:

$in{'Wholesale02'} = sprintf("%.3f", $in{'Wholesale02'});

change the 3f to 2f for two decimal places if you are not picky about having all numbers with decimal places (0.00, 1.50, 1.55).

If that is what you want, put it in under sub validate_records in db.cgi

Quote Reply
Re: Reg expression Syntax help pls In reply to
Here's a copy of a reference under "Syntax" in the FAQ noted below.

Allow specific number entry in a field
(.... i.e. 1.25, 1.50, 1.75, etc...)
so that the only allowable entries are from 0.25 to 24.00 in increments of .25 ?
================================================================

Forum reference: http://gossamer-threads.com/p/000598
----------------------------------------------------------------------------

Response:

You would have to add code to sub validate_record to account for this.
If you haven't already made changes to sub validate_record, replace it with the following code:

sub validate_record {
# --------------------------------------------------------
# Verifies that the information passed through the form and stored in %in matches a valid record. It checks first to see that if we are adding, that a duplicate ID key does not exist.
# It then checks to see that fields specified as not null are indeed not null, finally it checks against the reg expression given in the database definition.

my ($col, @input_err, $errstr, $err, $line, @lines, @data);
if ($in{'add_record'}) { # don't need to worry about duplicate key if modifying
open (DB, "<$db_file_name") or &cgierr("error in validate_records. unable to open db file: $db_file_name.\nReason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
(/^#/) and next LINE;
(/^\s*$/) and next LINE;
$line = $_; chomp ($line);
@data = &split_decode($line);
if ($data[$db_key_pos] eq $in{$db_key}) {
return "duplicate key error";
}
}
close DB;
}
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 ($col eq 'FieldName') {
if (($in{$col} < .25) or ($in{$col} > 24)) {
push(@input_err,"$col (not within range)");
}
unless ($in{$col}*100/25 == int($in{$col}*100/25)) {
push(@input_err,"$col (incorrect value)");
}
}
}
}

if ($#input_err+1 > 0) { # since there are errors, let's build
foreach $err (@input_err) { # a string listing the errors
$errstr .= "<li>$err"; # and return it.
}
return "<ul>$errstr</ul>";
}
else {
return "ok"; # no errors, return ok.
}
}

Notice that you will need to change FieldName to match the name of your field.<BR>
If you have already made changes to sub validate_record for some other reason you will need to compare above with
your current sub and make the appropriate changes.


Unoffical DBMan FAQ
http://webmagic.hypermart.net/dbman/
Quote Reply
Re: Reg expression Syntax help pls In reply to
Try this
Code:
/^\d{0,4}\.{0,1}\d{0,2}?$/
Happy Coding,

--Drew
http://www.FindingHim.com
Quote Reply
Re: Reg expression Syntax help pls In reply to
Nice idea but I get "Basic Time Claimed Incorrect format" message if I enter 37, here is the code I'm using in the sub validate:

if ($col eq 'Basic Time Claimed') {
($in{'Basic Time Claimed'} < 0 or $in{'Basic Time Claimed'} > 999.99) and
push(@input_err, "$col cannot be < '0' or > '999.99'");
}
if ($col eq 'Basic Time Claimed') {
($in{'Basic Time Claimed'} = sprintf("%.2f", $in{'Basic Time Claimed'})) and
push(@input_err, "$col Incorrect format");
}
I know I could "or" them together but I get the same problem.

What simple thing I am doing wrong??

Quote Reply
Re: Reg expression Syntax help pls In reply to
OK I have come up with something that is (i'm sure) longer than it has to be but it works....

It will match things like...... 1, 1.1, 1.11, 11.1, but not 100

/^(\d\d?)(\.)(\d?\d?)$|^(\d\d?)$/


This is how I tested it.......

#!/usr/bin/perl

print "Content-type: text/plain\n\n";

@num = ('2','2.2','2.22','22.2','99.99','200','200.2','200.22');


foreach (@num) {
if ($_ =~ /^(\d\d?)(\.)(\d?\d?)$|^(\d\d?)$/) {
print "$_ OK\n";
} else {
print "$_ Not OK\n";
}
}


Smile

Paul Wilson.
Installations:
http://www.wiredon.net/gt/