Gossamer Forum
Home : General : Perl Programming :

mailer to check valid number field

Quote Reply
mailer to check valid number field
Hi,

Sorry I know this is probably a really simple question for most, but perl is not yet one of my strong points.

I have a perl script running but have now added a telephone number field to my HTML web form. This script works fine with this but it allows a user to input any character not just numbers. I already have some field checking in place for empty fields or missing chars from an email field which redirects the user onto to an error page. I'd like this to happen if any characters other than digits are input into that field. Thanks if you can help, I greatly appreciate any help....

This is a piece of the code that validates and the variable is $Telephone (it already checks that something is input)

# _________________________________________________________


sub CheckEmailAddressFormat {
if (index($Email, "@") < 1) {&DoEmailError;}
if (index($Email, ".") < 1) {&DoEmailError;}
if (index($Email, " ") > -1) {&DoEmailError;}
}
sub CheckFields {
if (!$Forename || $Forename eq ' ') {&DoEmailError;}
if (!$Email || $Email eq ' ') {&DoEmailError;}
if (!$Telephone || $Telephone eq ' ') {&DoEmailError;}
}
sub DoEmailError {
print "Location: $ErrorPage\n\n";
exit;
}


# _________________________________________________________
Quote Reply
Re: [fevr6] mailer to check valid number field In reply to
Try this (this should allow digits and dashes):
Code:
DoEmailError() if $Telephone =~ /[^\d-]$/;

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] mailer to check valid number field In reply to
Thanks Yogi,

I inserted that into my code, I don't know if it's quite right what i've done.

sub CheckEmailAddressFormat {
if (index($Email, "@") < 1) {&DoEmailError;}
if (index($Email, ".") < 1) {&DoEmailError;}
if (index($Email, " ") > -1) {&DoEmailError;}
}
sub CheckFields {
if (!$Forename || $Forename eq ' ') {&DoEmailError;}
if (!$Email || $Email eq ' ') {&DoEmailError;}
if (!$Telephone || $Telephone eq ' ') {&DoEmailError;}
if (!$Telephone || $Telephone =~ /[^\d-]$/){&DoEmailError;}
}
sub DoEmailError {
print "Location: $ErrorPage\n\n";
exit;
}

What happens now is that it will work, but if I put a letter in between some numbers i.e 555we5897 it will accept it as valid and go on.
Quote Reply
Re: [fevr6] mailer to check valid number field In reply to
Your index()'s seem wrong. The first one causes an error if there is an @, the second if there is a .

index() returns -1 for no match.

Last edited by:

Paul: Nov 28, 2002, 2:36 AM
Quote Reply
Re: [Paul] mailer to check valid number field In reply to
Thanks paul,

I might need to look at that, but do you have any thoughts on where I might be going wrong with the $Telephone
Quote Reply
Re: [yogi] mailer to check valid number field In reply to
my bad, should be
Code:
if $Telephone =~ /[^\d-]/;
i.e. withouth the dollar sign...

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] mailer to check valid number field In reply to
Thanks Yogi, that did the trick, I slightly altered it to also accept spaces within the field. You saved me at the 11th hour.......Smile

Code:
sub CheckEmailAddressFormat {
if (index($Email, "@") < 1) {&DoEmailError;}
if (index($Email, ".") < 1) {&DoEmailError;}
if (index($Email, " ") > -1) {&DoEmailError;}
}
sub CheckFields {
if (!$Forename || $Forename eq ' ') {&DoEmailError;}
if (!$Email || $Email eq ' ') {&DoEmailError;}
if (!$Telephone || $Telephone eq ' ') {&DoEmailError;}
if (!$Telephone || $Telephone =~ /[^\d -]/){&DoEmailError;}
}
sub DoEmailError {
print "Location: $ErrorPage\n\n";
exit;
}

Last edited by:

Wil: Dec 2, 2002, 2:48 AM