473,508 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regular expression for numbers

33 New Member
I am new to regular expression. I have to validate an input, such as that should be non-negative numeric value (case 1) and non-negative numeric decimal value (case 2) (i.e. 0.5, 0.2, 10.0 etc) . I have tried out like below.

Expand|Select|Wrap|Line Numbers
  1.    var reg2 =   /^\d*\.{0,1}\d+$/;
  2.    if(!reg2.test(str)) {
  3.         alert("Not valid");
  4.    }
1. Is this enough to check for non negative numbers ?
2. How can I check for non negative decimal values ?
Aug 21 '07 #1
8 4787
acoder
16,027 Recognized Expert Moderator MVP
At the moment, your regular expression checks for:
  1. 0 or more digits (0-9)
  2. followed by 0 or 1 dots (there may not be a dot)
  3. followed by 1 or more digits
So, it would match 0, 5, 0.2, .4, 5.5, etc.
Aug 21 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, baskarpr.

Checking for a non-negative is easy:
Expand|Select|Wrap|Line Numbers
  1. val = parseFloat(str);
  2. if(val < 0)
  3. {
  4.     alert('Must not be negative.');
  5. }
  6.  
To check to make sure it has a decimal value, simply do this:
Expand|Select|Wrap|Line Numbers
  1. if(val == parseInt(val))
  2. {
  3.     alert('Must have a decimal part.');
  4. }
  5.  
Aug 21 '07 #3
baskarpr
33 New Member
I am new to regular expression. I have to validate an input, such as that should be non-negative numeric value (case 1) and non-negative numeric decimal value (case 2) (i.e. 0.5, 0.2, 10.0 etc) . I have tried out like below.


Expand|Select|Wrap|Line Numbers
  1. var reg2 =   /^\d*\.{0,1}\d+$/;
  2.    if(!reg2.test(str)) {
  3.         alert("Not valid");
  4.    }

1. Is this enough to check for non negative numbers ?
2. How can I check for non negative decimal values ?
Aug 21 '07 #4
baskarpr
33 New Member
Heya, baskarpr.

Checking for a non-negative is easy:
Expand|Select|Wrap|Line Numbers
  1. val = parseFloat(str);
  2. if(val < 0)
  3. {
  4.     alert('Must not be negative.');
  5. }
  6.  
To check to make sure it has a decimal value, simply do this:
Expand|Select|Wrap|Line Numbers
  1. if(val == parseInt(val))
  2. {
  3.     alert('Must have a decimal part.');
  4. }
  5.  

I have tried with "< 0 " for negative numbers. But just I wanted to do that check in regular expression fo uniformity. Now I found it. Thanks to every one replied. :-)
Aug 21 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, baskarpr.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 21 '07 #6
pbmods
5,821 Recognized Expert Expert
Merged duplicate threads.
Aug 21 '07 #7
baskarpr
33 New Member
Merged duplicate threads.
hi, pbmods.. thanks for reply... Actually when i submitted and came back, I cant find my post. So I posted again. seems to be a bug... :-)
Aug 22 '07 #8
pbmods
5,821 Recognized Expert Expert
Heya, Baskarpr.

Actually when i submitted and came back, I cant find my post.
Click on the 'Control Panel' link in the top right of the page when you are logged in.
Aug 22 '07 #9

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

Similar topics

1
4155
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
0
311
by: Mike | last post by:
I'm trying to come up with a regular expression that matches numbers with commas... but doesn't match numbers formatted without commas. The problem is that I'm trying to parse a file where the...
4
1746
by: Arne | last post by:
Hi, I'm trying to recognise phone numbers on the following format using regular expressions: +47 123 45 678 This should be recognised by the following regular expression: +47 \d{3} \d{2}...
3
7622
by: Robert Scheer | last post by:
Hi. I have a regularexpression validator control on a page. This regular expression validates a textbox to accept only numbers and commas: validationexpression="*" I am trying to modify this...
3
3200
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
14
11353
by: olekristianvillabo | last post by:
I have a regular expression that is approximately 100k bytes. (It is basically a list of all known norwegian postal numbers and the corresponding place with | in between. I know this is not the...
3
2552
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular...
10
1645
by: venugopal.sjce | last post by:
Hi Friends, I'm constructing a regular expression for validating an expression which looks as any of the following forms: 1. =4*++2 OR 2. =Sum()*6 Some of the samples I have constructed...
8
2768
by: cokofreedom | last post by:
I was reading up on this site http://www.noulakaz.net/weblog/ 2007/03/18/a-regular-expression-to-check-for-prime-numbers/] of an interesting way to work out prime numbers using Regular Expression....
3
1092
by: dmalhotr2001 | last post by:
Hi, I would like to know how to write regular expression in vb.net that would be valid for a piece of text that is only numbers, letters, dashes, and underscores that is allowed. Thanks :D
0
7324
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
7382
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...
1
7042
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
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.