473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Accept only numeric value

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
Jun 28 '07 #1
10 3582
KevinADC
4,059 Expert 2GB
What have you tried so far?
Jun 28 '07 #2
miller
1,089 Expert 1GB
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
Jun 28 '07 #3
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
Jun 28 '07 #4
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
Jul 2 '07 #5
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
Jul 2 '07 #6
KevinADC
4,059 Expert 2GB
You need to use "anchors" in your regexp:

Expand|Select|Wrap|Line Numbers
  1. if ( $num !~ /^[0-9]$)
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
Jul 2 '07 #7
For matching 1 or more then one digit use " * "


if ($num !~ /^[0-9]+$/) {
do something
}
Aug 25 '08 #8
Ganon11
3,652 Expert 2GB
Couldn't you also match for any non-numeric character?

Expand|Select|Wrap|Line Numbers
  1. if ($num =~ /[^0-9]/) { # If $num matches for any character that isn't 0-9
Aug 25 '08 #9
KevinADC
4,059 Expert 2GB
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.
Aug 25 '08 #10
KevinADC
4,059 Expert 2GB
Couldn't you also match for any non-numeric character?

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. if ($num =~ /\D/) {
  2.     print "Found a non-digit";
  3. }
  4.  
\D is the compliment of \d
Aug 25 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
3
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...
2
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...
6
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...
8
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...
1
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...
0
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...
2
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...
8
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...
5
lotus18
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.