Gossamer Forum
Home : General : Perl Programming :

Validate Input

Quote Reply
Validate Input
I need to validate the input for three types of fields. I am sure someone out there already has the syntax.

The three field types are:

Telephone Number

Email

5-Digit Zipcode

Thanks for the help.

Lee
Quote Reply
Re: [Lee] Validate Input In reply to
zip code is easy..

=~ /^\d{5}$/

....what format should the phone number be in?

You can use Email::Valid to check emails or a simple regex is:

=~ /^\S+@\S+\.\S+$/

Last edited by:

Paul: Jul 26, 2002, 9:15 AM
Quote Reply
Re: [Paul] Validate Input In reply to
Wow ... that was a fast response!

For telephone, I suppose what I need is just to check to see that (, ), -, and 0-9 are the only characters allowed. I'm doing a project for a corporation that has extensions that can be used for the phone number. So, a person could validly input the following, all the following being "legal":

(123) 456-7890

123-456-7890

1234567890

890
Quote Reply
Re: [Lee] Validate Input In reply to
>>
(123) 456-7890

123-456-7890

1234567890

890
<<

Try:

=~ /^\(?\d{3}\)?(?:[\s\-]*\d{3}[\s\-]*\d{4})?$/;

That *should* match any of the above, plus 123 456 7890 and (123) 456 7890

Last edited by:

Paul: Jul 26, 2002, 10:11 AM
Quote Reply
Re: [Paul] Validate Input In reply to
it'll also match..

123)--------------------------------------------------------4567890


or something like that.. Wink

Last edited by:

widgetz: Jul 29, 2002, 6:56 PM
Quote Reply
Re: [widgetz] Validate Input In reply to
That doesn't work - gives me an error.

Can someone please check and see or if there is a better solution:

elsif (($fields{daytel} eq "") || ($fields{daytel} =~ /^\(?\d{3}\)?(?:[\s\-]*\d{3}[\s\-]*\d{4})?$/) || $daytel_length < 9)
{
print "<h4>Your phone number is invalid. <A HREF=\"javascript:history.back()\">Go Back</A></h4>\n";
}

Thanks
Quote Reply
Re: [socrates] Validate Input In reply to
How about having each part of the phone number as a seperate input? then a simple variation of ^\d{3}$ will do for each part. Definately not an elegant approach, but it is a perfectly valid and simple solution.

Here's a dirty solution for you...

Code:
$num = "aldkj654658djkd5465";
$num =~ s,\D,,g;
$num = substr($num, 0, 10);

if ($num !~ /^(\d{7}|\d{10})$/) {
print "your phone number appears invalid";
}

it strips out any non-digit, then takes the first 10 digits from that. Then it tests that there are EXACTLY 7 or 10 digits. Of course the test is brain-dead when it comes to telling the difference between a fake phone number (like 000-0000) and a real one.

Philip
------------------
Limecat is not pleased.

Last edited by:

fuzzy logic: Aug 3, 2006, 12:41 AM
Quote Reply
Re: [fuzzy logic] Validate Input In reply to
Thanks Philip but what is the follwing supposed to do?
$num = "aldkj654658djkd5465";


I had to remove it for it to work - wouldn't work otherwise.

To detect more than 3 repititive digits I added :

elsif ($num !~ /^(\d{7}|\d{10})$/ || $num =~
(/([0-9]{2,})\1/)) {

Last edited by:

socrates: Aug 4, 2006, 6:45 PM
Quote Reply
Re: [socrates] Validate Input In reply to
That was just an example input variable for you to see what kind of "junk" input could be used, and still get a valid phone number from it. As an afterthought, you don't really want to use substr() as that's being too forgiving trying to make the input valid.

Philip
------------------
Limecat is not pleased.