LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 01-11-2007, 12:01 PM   #1
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Rep: Reputation: 15
perl/cgi version of VBA's isnumeric


I have been looking for an example of how to validate data coming in from a form and make sure that what was entered in the form is made up of numbers (positive or negative) and not a character. I would have thought that this would have been a common thing, but I can not seem to find it. Can someone help me?
 
Old 01-11-2007, 02:07 PM   #2
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
solution

A friend just emailed me this code and if it helps others great, it seemed to do the trick unless anyone can see any problems with it. I am not very good with Regular Expressions so I am not even sure I can explain it, but I added in the notes as best I could.

Code:
if ($temp eq "" || IsNumeric($temp) == 0) {
    return "N";     #Data is not valid - empty string/it is something other than a +/- number
} else {
    return "Y";     #Data is valid - it is a +/- number
}

#***** User Defined Functions ******
sub IsNumeric {
    $val = $_[0];
    if ($val !~/^[0-9|.|,|-]*$/) {
        return 0;   #Non-numeric
    } else {
        return 1;   #Numeric
    }
}  #End IsNumeric
 
Old 01-11-2007, 05:56 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.

Here is a test file that is intended to exercise numeric string recognition:
Code:
% cat data1
# Thu Jan 11 14:57:08 CST 2007
# Tests for numeric.  The first line is empty.

3
33
3.3
3.
.3
3.3.3.3.3
+3
3+
-3
3-
|3|
|,.-|
|||||
Here are the results from the posted code:
Code:
% ./p1 data1
 not numeric :# Thu Jan 11 14:57:08 CST 2007:
 not numeric :# Tests for numeric.  The first line is empty.:
 is  numeric ::
 is  numeric :3:
 is  numeric :33:
 is  numeric :3.3:
 is  numeric :3.:
 is  numeric :.3:
 is  numeric :3.3.3.3.3:
 not numeric :+3:
 not numeric :3+:
 is  numeric :-3:
 is  numeric :3-:
 is  numeric :|3|:
 is  numeric :|,.-|:
 is  numeric :|||||:
 ( Lines read: 16 )
Here is a alternate function:
Code:
% cat p2
#!/usr/bin/perl

# @(#) p2       Demonstrate numeric regular expression, decimal.
# $Id$

# See pages 261, Perl Best Practices, O'Reilly

use warnings;
use strict;


my $lines = 0;
while ( <> ) {
        $lines++;
        chomp;
        if ( not IsNumeric ( $_ ) ) {
                print " not numeric :$_:\n";
        } else {
                print " is  numeric :$_:\n";
        }
}

print STDERR " ( Lines read: $lines )\n";

sub IsNumeric {
    my $val = $_[0];
        my $DIGITS = qr{ \d+ (?: [.] \d*)? | [.] \d+ }xms;
        my $SIGN   = qr{ [+-] }xms;
        my $NUMBER = qr{ ($SIGN?) ($DIGITS) }xms;
        if ( $val !~ /^${NUMBER}$/ ) {
        return 0;   #Non-numeric
    } else {
        return 1;   #Numeric
    }
}  #End IsNumeric

exit(0);
Here are the results from the alternate function:
Code:
% ./p2 data1
 not numeric :# Thu Jan 11 14:57:08 CST 2007:
 not numeric :# Tests for numeric.  The first line is empty.:
 not numeric ::
 is  numeric :3:
 is  numeric :33:
 is  numeric :3.3:
 is  numeric :3.:
 is  numeric :.3:
 not numeric :3.3.3.3.3:
 is  numeric :+3:
 not numeric :3+:
 is  numeric :-3:
 not numeric :3-:
 not numeric :|3|:
 not numeric :|,.-|:
 not numeric :|||||:
 ( Lines read: 16 )
Best wishes ... cheers, makyo
 
Old 01-12-2007, 09:35 AM   #4
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks, it looks very complicated to me right now and a bit more thorough than what I posted. I image that it will make more sense once I get the Regular Expressions under my belt. Any suggestions on a place to start learning about the regular expressions?

Thanks for the reply!
 
Old 01-12-2007, 10:19 AM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 706

Rep: Reputation: 68
Hi.

For learning, I suggest you use Google to search for "regular expression tutorial" and glance at the top 10 or so hits of the 800K that you get.

However, I find that I need to augment reading with practice ... cheers, makyo
 
  


Reply

Tags
expressions, numeric, regular


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl CGI:Can't locate CGI.pm supermyself Programming 13 09-10-2007 06:22 AM
CGI/Perl indienick Programming 2 03-06-2006 03:16 PM
C equivalent of Java's isnumeric zaichik Programming 3 02-08-2005 08:06 PM
cgi perl : I cant get perl to append my html file... the_y_man Programming 3 03-22-2004 05:07 AM
cgi+perl barbanero General 0 04-07-2002 12:48 PM


All times are GMT -5. The time now is 02:46 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration