good chemistry is complicated,and a little bit messy -LW PerlMonks

### How can I validate user input?

by Buckaroo Buddha (Scribe)
 on May 10, 2000 at 19:40 UTC Need Help??
Contributed by Buckaroo Buddha on May 10, 2000 at 19:40 UTC
Q&A  > input and output

#### Description:

I've tried:
```\$start = \$ARGV[0];

if (\$start eq '') {
\$start = &get_start_level;
}
sub get_start_level {
print "Please Enter the Start-Level (0-7,?)[0]: ";
\$input = <>;
# clear all but numbers or '?'
#if (\$input ne '?') {
#    \$input =~ tr/!(0-9)//\d;
#}

if (\$input eq '?') {
print " **** internally coded help information  **** \n";
print " **** internally coded help information  **** \n";
\$input = &get_start_level;
} elsif ((\$input < 0) || (\$input > 7)) {
print "please enter a number that is between Zero and Seven\n";
\$input = &get_start_level;
}
return (\$input);
}
=========================== end code  ============
This seems to me like a pretty straight forward problem. What am i doing wrong? ... I can't seem to figure this out.

 Answer: how do i work thiscontributed by Russ Here's one way to do this: ```my \$start = \$ARGV[0]; while (1){ print "Please Enter the Start-Level (0-7,?)[0]: "; chomp(\$start = <>); # Check for non-numeric characters if (\$start =~ /\D/){ print "\nPlease enter a number (0-7) or ? for help\n"; next; } last if 0 <= \$start and \$start <= 7; # Check for a help request if (\$start eq '?') { print " **** internally coded help information **** \n"; next; } print "\nPlease enter a number between Zero and Seven, inclusive\n"; } print \$start; [download]``` First, we check that the input is numeric (as you started to do in your commented-out code) to prevent errors if the user enters non-numeric characters. If the input is valid, we just exit the loop. If not, check for a help request and, if not '?', print a generic error message and redo the loop. There are a million different ways to do this, and this is only one of them... Russ Answer: How can I validate user input?contributed by ton ```while (\$start !~ /^[0-7]\$/) { print "Please Enter the Start-Level (0-7,?)[0]: "; \$start = ; chomp \$start; if (\$start eq '?') { print "Some help info.\n"; } } [download]```

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-23 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (125 votes). Check out past polls.

Notices?