Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Validation 3

Status
Not open for further replies.

wood

MIS
Aug 3, 2000
139
CA
I am new to perl, and I need to validate a number that is entered. It needs to be in this format, ###.#. It can have up to 3 digits before the decimal and one after.

I am trying: /\d\d\d\.\d/ but that doesn't seem to work. It allows 1234, and 123.45 through.

Could someone please assist me. Thanks!
 
your pattern matching is testing for ###.# ANYWHERE in the number - 123.45 matches that. You need to say that ###.# is the, basically, "from start to finish", so you do /^\d\d\d\.\d$/

^ means basically "begins here" and $ means "ends here"
 
Ok, that worked for 123.45, but it still allows 1234 or 12.23. Any suggestions?

I am trying to validate that the number can have 3 digits before the decimal point and one after.

Thanks!
 
I'd suggest range checking
Code:
if (($number >= -1000) and ($number<=1000)) {
  print "Winner alright";
} else {
  print "outta bounds!";
}
HTH
--Paul

cigless ...
 
Ok, that works for the 1234. Thank you for your suggestion.

How do I stop the person from entering more than one decimal place?
 
err...Wood, I think you may have mistyped something. I have just tried the code I gave you, and it works

-------
my $number = <STDIN>;
chomp $number;
print "Number entered is: *$number*\n";
($number =~ /^\d\d\d\.\d$/) ? print "Validated!\n" : print "NOT Validated\n";
-------

This will validate ONLY if the format is as you suggested (digit,digit,digit,perriod,digit)
 
Thank you both for your help. I figured out what I needed:


if (! (($ARGS{mileage} =~ /^\d\.\d$/ ) | ($ARGS{mileage} =~ /^\d\d\.\d$/ ) | ($ARGS{mileage} =~ /^\d\d\d\.\d$/ ) | ($ARGS{mileage} =~ /^\d\d\d$/ )))

because the person can enter 123 or 1.2 or 12.3 or 123.4 but not 123.45.

Thanks again!
 
a cleaner way to do that would be

if (! #ARGS{$mileage} =~ /^\d{1,3}\.\d$/)
 
I suspect azzazzello has typoed. Should be:
Code:
if ( ! $ARGS{$mileage} =~ /^\d{1,3}\.\d$/ )

wood - in the expression you posted, you should be using the logical OR operator `||', rather than its bitwise equivalent `|'. They'll work similarly in some situations but are not the same.
 
wood
because the person can enter 123 or 1.2 or 12.3 or 123.4 but not 123.45.

you need something like this
Code:
=~ /^\d{1,3}\.?\d$/;

the '?' goes because you want to accept numbers like 123 , so we match an optional '.'
without '?' if he enters 123 it will say that it is not valid.

TIMTOWTDI
 
@pengo
I'd change that slightly again. Yours won't allow single-digit integers, so the last digit should be optional too:
Code:
=~ /^\d{1,3}(?:\.\d)?$/;
 
Thanks ishnid. That worked the way I wanted.

A BIG thank you to all of those who helped me. I appreciate you assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top