Gossamer Forum
Home : Products : DBMan : Customization :

Text box validation problem

Quote Reply
Text box validation problem
I have a proplem which some one may have come across before or have a fix for.
The problem I'm having is that of long continious strings in a text box, ie if some one holds down a key and enters a 100 continious charactors.
When the field is returned it tries to print the continious string in one line, which totally distroys the table formatting, turning the page into a complete mess.
I,ve asked the guys at the javascript forum for a validation script, but I would like to also include perl validation as well for this , as javascript is not very secure and can be easially bypassed by those who vandalise.
The script would have to check each word with in the text box, and return an error if over a certain length.
Can any body help.
Thanks
Bob
Quote Reply
Re: Text box validation problem In reply to
Thats just, what I'm wrestling with for days, but no satisfying solution.
Anyone???
Quote Reply
Re: Text box validation problem In reply to
Your sub validate in db.cgi should look like this.

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);
#####################################
# Place your code here. Replace $in{'your_field'} with the name of the field you are working on. Also replace 15 with the first number that is too long. IE: If 50 characters is the longest acceptable word, put 51.

@words = split(/\s/,$in{'your_field'});
foreach (@words) {
unless ($_ =~ /^.{1,15}$/) {
push(@input_err, "$col (You have words that are too long!)");last;
}
}


####################################
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 ($#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.
}
}

------------------
Chris



[This message has been edited by Chris071371 (edited January 13, 2000).]
Quote Reply
Re: Text box validation problem In reply to
Thanks Chris I'll give that a try.
Bob