CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
I'll try again...

 



monocle
User

Jun 14, 2000, 10:41 PM

Post #1 of 7 (967 views)
I'll try again... Can't Post

I can't get seem to get the line of code to check that a Form Field contains ONLY numbers, nothing else. No letters, No spaces...Only numbers.

I've tried several examples found in earlier posts on the boards but nothing is working for me.

Any body have a suggestion?

Thanks,
Monocle


perlkid
stranger

Jun 15, 2000, 3:12 PM

Post #2 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

 
Try this

if ($form{'field'} =~ /[0-9]/)
{
print "Only Numbers";
}
else
{
print "Other Than Only Numbers";
}

I'm using that code in a script also so it should work fine.

perlkid


Kanji
User

Jun 15, 2000, 3:34 PM

Post #3 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

Uh ... no.

That snippet of code would tell you if the variable contained a number because your regexp isn't anchored (ie, "A1 Steak Sauce" would be considered a number because it contains a 1).

Instead, you'd want something like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

if ( $field =~ /^\d+$/ ) { ... }
else { ... }</pre><HR></BLOCKQUOTE>

... but that only works for whole numbers. If you want negative numbers, decimals, etc. you'd need to modify the regexp accordingly ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

/^(-?(\d+|\d+\.\d+)$/</pre><HR></BLOCKQUOTE>

... which considers 1, 0.1, and -22 all valid numbers.


monocle
User

Jun 15, 2000, 3:45 PM

Post #4 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

Thanks Kanji,

The first one was perfect. I only needed to make sure that a zip code had only numbers to prevent errors later on.

Its funny...I think I was really close. I kept trying different things and some of them looked close to that...but close don't cut it!

Thanks again,
Monocle


Kanji
User

Jun 15, 2000, 3:57 PM

Post #5 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

International considerations aside, what happens if you encounter ZIP+4? Smile

You probably want /^\d{5}(-\d{4})$/ for ZIPs, which expects 5 digits optionally followed by a dash and another 4 digits.


monocle
User

Jun 16, 2000, 8:11 AM

Post #6 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

Kanji,
Thanks for your concern, however I have handled that by having a seperate field for the ZIP and another for the +4. Plus, I am only handling US addresses at this point.

Actual, I am nearly finished debugging a script that will queary the USPS database to get the +4 part if it was left blank and fill it in, since most people have no clue what that is for their address.

Monocle


Cure
User

Jun 16, 2000, 6:08 PM

Post #7 of 7 (967 views)
Re: I'll try again... [In reply to] Can't Post

\d means match a digit character
+ means match 1 or more time
* means match 0 or more times
$ means match at the end of a string of
before a newline character
^ means match at the beginning of a string
\d+ means match one or more digit character
\d* means match zero or more digit character

if ($q->param('name_of_form_filed) =~^\d+$) if this statement is true then the form field only contains digits heres another way to do that
if ($q->param('hame_of_form_field) =~^[0-9]+$)

bye the way use CGI.pm to parse your data
if you have perl installed on your system then go to your command line and type perldoc CGI
and that will give you info. about the CGI module and type perldoc perlre and that will explain regular expressions

good luck and if you have any (?)s just ask

Cure

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives