ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?
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
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 )
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.