[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to validate input?

Posted on 2006-04-19
12
Medium Priority
?
296 Views
Last Modified: 2012-06-27
Dear experts, I have a web page, cusotmer will input their first, last name, e-mail address and phone number. It looks like this:

To add a new contact, please fill in all fields and click "Save".
Please fill out the fields below.

First Name:    
 
Last Name:    
 
Email Address:    
 
Phone Number:    
Phone number must be in the form (555)555-5555.

\\How can I use Perl to validate customer name, e-mail address and phone number after customer click save? and return a message like this:

New Contact Added!

First Name:  Alan  
 
Last Name:  Smith  
 
Email Address:  alan.smith@customer.com  
 
Phone Number:  555-555-5555  
 
\\ Please show me the code and how to link to my web page. Thank you.


 
0
Comment
Question by:kiirowei
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:jhurst
ID: 16490522
well clear this is why perl is ideal - regular expressions would allow you to do whatever you need.

So, I guess we are missing something here.  If you understand perl then you must understand regular expressions.  

What am I missing here.
0
 

Author Comment

by:kiirowei
ID: 16490607
?? I'm sorry jhurst, I didn't get what you said ??
So, how should I use Perl to validate those informations? I think I need to link the save button to .pl file. Can soemone show me the .pl file?
0
 
LVL 85

Expert Comment

by:ozo
ID: 16490726
use Email::Valid;
unless( Email::Valid->address($email) ){
    print "invalid address $email\n" ;
}
unless( $phone=~/^(\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/ ){
    print "$phone not in the form (555)555-5555 or 555-555-5555\n";
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:kiirowei
ID: 16490962
ozo, thank you so much for helping me.
I still have some questions:
1. How about first name and last name? how should I validate those?
2. Also how should I add this to my HTML page? I know I can add javascript by using this:
<SCRIPT language=javascript src="mycode.js" type=text/javascript></SCRIPT>
But how can I add Perl?
3. So I can save what you worte into a .pl file, and it should validate?


0
 

Author Comment

by:kiirowei
ID: 16491025
Alos I need the script return a message like this:

New Contact Added!

First Name:  Joe  
 
Last Name:  Smith  
 
Email Address:  joe.smith@isp.com  
 
Phone Number:  555-555-5555  
 
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16491033
1.
how many first and last names do you have?
I guess that this is a useless question (unless you define which are valid characters and which are valid sequences of these characters for each name)

2.
you will not add to the HTML page, but to the script which gets the data back
don't use JavaScript for these checks, it is not reliable at all

3.
 yes and no, you should write it in your file which parses the input
0
 

Author Comment

by:kiirowei
ID: 16491211
Thanks for helping guys, I think I need to clear what I want.
I have a HTML page, after customer input names, e-mail address and phone number, they will click save.
After they clcik save I need a Perl file that validate the inputs, and return a message like this:

New Contact Added!

First Name:  Joe  
 
Last Name:  Smith  
 
Email Address:  joe.smith@isp.com  
 
Phone Number:  555-555-5555  

So I need a Perl file that validate and return the message, and how to link my HTML to that Perl file.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16492066
> I have a HTML page, ..
so you have something in there like:

  <form method=post action="/path/to/script.cgi" >

then you have to write that script.cgi (in perl as the TA suggests:)
This script needs to read the values and write a new HTML page as response.
Writing this script is not done by experts, you need to do it yourself, but we can assist if you need to solve some problems.

With perl you most likely use CGI.pm to deal with input and write the HTML page, please get used to that first:
  man CGI
0
 
LVL 8

Expert Comment

by:jhurst
ID: 16493685
you can not add perl to your page since the html page is processed on the client's machine and there may bot be perl there.  Actually, even if there was it would be a hige security risk to allow it to be involed my a web page.
0
 
LVL 5

Accepted Solution

by:
ZiaTioN earned 2000 total points
ID: 16509918
> you can not add perl to your page since the html page is processed on the client's machine and there may bot be perl there.  Actually, even if there was it would be a
>hige security risk to allow it to be involed my a web page.

Uhh what?



In html page add:

---------------------------------------------------------
<form method="post" action="/cgi-bin/validate">
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
Email Address: <input type="text" name="email" value="">
Phone Number: <input type="text" name="phone" value="">
</form>
---------------------------------------------------------

Create a script called "validate" in your cgi-bin folder and set permissions to 0755.
In this script put the following code:

---------------------------------------------------------
#!/usr/bun/perl -w

use strict;
use CGI qw(:standard);

my %parms;
$parms{$_} = param($_) for (param());

my @fields = qw(fname lname email phone);

for (@fields) {
   error("Missing required field!\n") unless ($parms{$_});
}

error('Invalid email detected')
   unless ($parms{'email'} =~ /^[\w\-\_\.]+\@[\w\-\_\.]+\.[\w\-\_\.]+$/);

error('Invalid telephone number detected. Format must be: (555)555-5555')
   unless ($parms{'phone'} =~ /\(\d{3}\)\d{3}\-\d{4}/);

success();

sub error {
   my $errormsg = shift;
   print header();
   print qq~
            There was an error detected.
            Error message given is: <span style="color: red;">$errormsg</span>
           ~;
   exit(0);
}

sub success {
   print header();
   print qq~
            New Contact Added!

            First Name:  $parms{'fname'}  
            Last Name:  $parms{'lname'}  
            Email Address:  $parms{'email'}
            Phone Number:  $parms{'phone'}  
           ~;
}
---------------------------------------------------------
0
 
LVL 8

Expert Comment

by:jhurst
ID: 16520027
re the Uhh What?

As he demonstrates - you CAN NOT add perl to your web page.  

What he demonstrates is that if you have a client machine where perl is present and installed, then you could create a web page that uses some perl that you could create on that machine.  This is VERY different to including perl on your web page.  I really dount that you wnat all of your visitors to need to install perl and your perl script so that the page works the way you want.  Even if you did, I doubt that they would.
0
 
LVL 5

Expert Comment

by:ZiaTioN
ID: 16525376
Please do not listen to jhurst, he hasn't a clue what he is talking about.

The perl code goes on the server side, not the client side.  What I demonstrated is not even close to what you said at all.  What I demonstrated was the fact that you can write a perl/cgi script and run it on your SERVER and not client and call it from a form tag as the action argument.  This does not require any user to have perl or even know what perl is.  This is the same for PHP, ASP, C, or any other language that can be used as a scripting language.

I used a form tag in your example because that is what you were asking for but you can also use SSI's (Server Side Includes) to call any web application and have it on your web page.  No offense jhurst but if you do not known what you are talking about, you really should not be so adimate about it.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question