Hi,
Please let me know how I can accomplish the following task -
"Ask the user to enter a number".
Check whether the user has entered only numeric values. If he/she enters any character other than 0-9, it should print out an error.
Thanks,
Karthik
10 3582
What have you tried so far?
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.
Then when you are ready post a new question in this thread.
MODERATOR
karthick
go to w3schools or tizag.com and int that sites go through the javascript tutorial if u need to validate the form field only with numbers or else try to copy a sample code ur doing now in to this forum.
So that people can help u clearly..
regards,
Lastknight
What have you tried so far?
Hi Kevin,
This is the program I have written so far..
#!/usr/bin/perl
use strict;
use warnings;
print "Enter a numeric value\n";
my $num = <STDIN>;
chomp($num);
if ( $num !~ /[0-9])
{
print "Enter a numeric value only\n";
}
else
print "Numeric value entered is $num\n";
But this program accepts values like '99a, a99' etc..
Cheers,
Karthik
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.
Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.
Then when you are ready post a new question in this thread.
MODERATOR
Thanks for the info Miller...I had written the program..but was too lazy to post it out here..sorry..I have posted it now.. ;)
Thanks,
Karthik
You need to use "anchors" in your regexp:
With all of the regexps above, if the regexp matched anywhere in the string, it was considered a match. Sometimes, however, we'd like to specify where in the string the regexp should try to match. To do this, we would use the anchor metacharacters ^ and $ . The anchor ^ means match at the beginning of the string and the anchor $ means match at the end of the string, or before a newline at the end of the string. Here is how they are used:
You can find the above in this regexp tutorial: http://perldoc.perl.org/perlretut.html
somewhere in Part 1. The Basics
For matching 1 or more then one digit use " * "
if ($num !~ /^[0-9]+$/) {
do something
}
Couldn't you also match for any non-numeric character? - if ($num =~ /[^0-9]/) { # If $num matches for any character that isn't 0-9
For matching 1 or more then one digit use " * "
if ($num !~ /^[0-9]+$/) {
do something
}
I guess you meant "+", which is correct, not "*". Please note the date of threads. You have posted in a very old one.
Couldn't you also match for any non-numeric character? - if ($num =~ /[^0-9]/) { # If $num matches for any character that isn't 0-9
You could but that is sort of twisted logic. It is also more clearly written like this: - if ($num =~ /\D/) {
-
print "Found a non-digit";
-
}
-
\D is the compliment of \d
Sign in to post your reply or Sign up for a free account.
Similar topics
by: S. van Beek |
last post by:
Dear reader,
How can I filter a numeric field with Like as criteria in a query.
To filter a numeric field with <10 as criteria this will com back with the
result of those records for which...
|
by: Maqsood Ahmed |
last post by:
Hello,
Here is the scenario:
I have a datagrid on a winform and a datatable is set as its
datasource. I have added DataGridTableStyle in datagrid's TableStyles
property. Some of Gridstyles are...
|
by: benjamin_r |
last post by:
I am trying to use the std::accumulate function. When I compile the
following code I get: error C2780: '_Ty
std::accumulate(_InIt,_InIt,_Ty,_Fn2)' : expects 4 arguments - 3 provided.
It should...
|
by: M.A. Oude Kotte |
last post by:
Hi All,
I hope this is the correct mailing list for this question. But neither
postgresql.org nor google could help me out on this subject.
I did find one disturbing topic on the mailing list...
|
by: .Net Sports |
last post by:
I am checking for text input on a form validation in javascript that
required at least one numeric character along with any number of alpha
characters for a given input text box. The below is a var...
|
by: karthik2423 |
last post by:
Hi,
Please let me know how I can accomplish the following task -
"Ask the user to enter a number".
Check whether the user has entered only numeric values. If he/she enters any character other...
|
by: ronysk |
last post by:
Hi,
I am posting here to seek for help on type conversion between Python (Numeric Python) and C.
Attachment A is a math function written in C, which is called by a Python program. I had...
|
by: tron_23 |
last post by:
hi,
we use Toplink (TopLink - 4.6.0 (Build 417) with a DB2 Database 7.2. i
know really old versions, but we could change to e newer one ;-)
Sometimes we got some problems with update or insert...
|
by: Frank Swarbrick |
last post by:
My DBA says that a column defined, for instance, as DECIMAL(11,2) and
containing a value of 1.00 takes up no more space on the database disk than
a column defined as DECIMAL(7,2) and containing a...
|
by: lotus18 |
last post by:
Hello World!
I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters.
Public Enum MyOption
Alpha = 1
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |