Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Field Verification 1

Status
Not open for further replies.

salc76

Technical User
Feb 21, 2007
21
CA
How can I force users to enter specific characters or number of characters on a field in a form using CGI?

Basically, limit users to only enter specific characters in a field. i.e.

Enter ZIP: 00000

Referring to the above example, if a user enters an alpha or more then 5 characters then the user will be prompted with an error.

Any help is greatly appreciated,

Thanks

 
Well, you can limit the maximum number of characters allowed by putting the HTML attribute of maxlength on the field.

As for which characters they can enter, well that can be done multiple ways:

[ul]
[li]JavaScript based solution which on field update/blur/etc checks the value against a known set of allowed characters. This will work for the percentage of your user base that has JavaScript turned on.[/li]
[li]Server side checking in the code[/li]
[/ul]

Personally, I'd suggest a combination of both client-side (JavaScript) and server side validation to ensure you get 100% of your users.

- George
 
Use maxlength in the form field but don't rely on it for server side purposes. The form can be edited and the maxlength attribute changed or removed entirely.

As far as server side validation there are a few ways to go. Here are some basic checks:

Code:
[olive][b]if[/b][/olive] [red]([/red]![blue]$zip[/blue] || [url=http://perldoc.perl.org/functions/length.html][black][b]length[/b][/black][/url][red]([/red][blue]$zip[/blue][red])[/red] != [fuchsia]5[/fuchsia] || [blue]$zip[/blue] =~ [red]/[/red][purple][purple][b]\D[/b][/purple][/purple][red]/[/red][red])[/red] [red]{[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]no good. zip is undefined, as too few or too many characters, or has a non-digit character[/purple][red]"[/red][red];[/red]
[red]}[/red]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Agree with Kevin, but it's important enough to repeat: NEVER rely exclusively on client-side checking for the integrity of the data you receive. It's extremely easy to bypass. It is often worth doing to save on the server load and speed things up (i.e. for regular users, no request will be made on the server if they enter something incorrectly: they'll immediately get a client-side warning) but you should always check on the server-side too.
 
Ok. this script allows a user to input data onto a fourm where the cgi script will record the data onto a flat file .txt file. What I would now like to do is impliment form validation. The script would validate the following form input. Can someone help me with this?

Part of the script:

#!/usr/bin/perl
use CGI qw/:standard/;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = new CGI;



my $name = $cgi->param('name');
my $staddress = $cgi->param('StreetAddress');
my $city = $cgi->param('City');
my $province = $cgi->param('Province');
my $postalcode = $cgi->param('postalcode');
my $phonenumber = $cgi->param('phonenumber');
my $email = $cgi->param('Email');


if (!$name ||length($name) !=5 || $name =~/\D/) {print"no good";}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top