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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Validation

Status
Not open for further replies.

Chivo

Programmer
Oct 25, 2006
15
0
0
GB
Hi all- newbie here.

I could be a regular by the time my CIW course is finished.

I have a query about data validation which I'm hoping will be bread and butter for someone. I've been asked to write a perl program to request input and check the response. The first of these is to ask for a 4 digit number and ensure it meets the criteria. This is something I thought would be easy but in practice it is not working.

I've posted my code below. It does ensure that only 4 digits are used and that the inout is numeric. It does not though stop a user from typing more than 4 digits:

print ("Please enter your 4 digit customer reference:\n");
$custRef =<STDIN>;
chomp($custRef);
while ($custRef !~ m/\d{4,4}/)
{
print("There was an error, try again.\n\n");
print ("Please enter your 4 digit customer reference:\n");
$custRef =<STDIN>;
chomp($custRef);
}
 
you need to use string anchors (^$) when you want to make sure a string is an exact match from beginning to end:

Code:
while ($custRef !~ m/^\d{4,4}$/)

The way you had it just made sure there were 4 digits anywhere in the string.

- Kevin, perl coder unexceptional!
 
Hi Kevin,

Thanks alot, it worked a treat. More than that though thanks for the explanation. My head was hurting. I knew what was going wrong but couldn't see how to fix it and googling didn't help.

I'll push on now but will probably be back at some point.

Thanks again.
 
Lots of good people here so ask all the questions you want. Just make sure to not post any school work assignments, otherwise you could be banned from the forum.

Students

Students are not allowed to post homework problems in the Tek-Tips forums for the purpose of getting answers to their homework. This is considered cheating. Offending posts will be removed from the site and offending members will lose their membership privileges.


If you are stuck with a course/school assignment and are asking for help getting over a hump that is OK, as long as you post the code and show that you have been making an effort and target your question to the specific problem, like you did in this thread. [smile]

- Kevin, perl coder unexceptional!
 
Lol no problem. I'm a little old for school now. I've been doing a course on Web Design to change careers and decided to go back and do some scenario questions on Perl. It's amazing how much I have forgotten in such a short space of time- a month. My notes are sparse and I'm really struggling to grasp the basics again as I haven't used Perl in between.

What you posted yesterday I understand so for now I can say some of it is coming back. :) I'd like to ask another question though if I may. I've sussed now, thanks to your help and some more reading, how to match an input using pattern matching. My query now concerns multiple choice. For example if I prompted for an inout of "yes", "no" or "maybe" I'm not sure the best way to employ matching.

Would I use an array of those strings to match the input or would I use an OR statement? Preferably I'd like to use [Yy]es for example to ensure both are acceptable.

Any hints would be appreciated. Apologies if this seems like basic stuff but I have done alot of refreshing since yesterday but I am still struggling with some of the concepts.

Cheers
 
Using the regex as you suggest makes the code less susceptible to bugs caused by variations in user input. You can use a
Code:
if (/regex1/) {
   #yes
}
elseif (/regex2/) {
   #no
}
elseif (/regex3/) {
   #maybe
}
else {
   # error message
}
type of structure, but as you can see things quickly get out of hand if you have more than a few options.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve....

"elesif" ? Have you been doing PHP? [noevil]

[wink]

- Kevin, perl coder unexceptional!
 
Aaargh.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top