473,666 Members | 2,116 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 4796
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
4167
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
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 columns run together sometimes. For example 987,456,321123,456,78 This matches for the following regular expression ?(\d|,)*\.?\ What I'd like to to is only match up to three numbers between commas so that the preceding example would come back...
4
1753
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} \d{3} but it's not. This regular expression: +47 \d{2} \d{2} \d{2} \d{2} DOES recognise the following phone number: +47 12 34 56 78
3
7628
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 expression to not allow commas at the beginning and at the end of the expression without success. It needs to allow commas only between the numbers. How can I do that?
3
3209
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
14
11437
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 intended use for regular expressions, but it should nonetheless work. the pattern is ur'(N-|NO-)?(5259 HJELLESTAD|4026 STAVANGER|4027 STAVANGER........|8305 SVOLVÆR)' The error message I get is:
3
2562
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 expression. ^(.+?) uses (?!a spoon)\.$
10
1664
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 below:
8
2773
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. However my attempts to use this in Python keep returning none (obviously no match), however I don't see why, I was under the impression Python used the same RE system as Perl/Ruby and I know the convert is producing the correct display of...
3
1096
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
8871
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.