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. -
var reg2 = /^\d*\.{0,1}\d+$/;
-
if(!reg2.test(str)) {
-
alert("Not valid");
-
}
1. Is this enough to check for non negative numbers ?
2. How can I check for non negative decimal values ?
8 4796 acoder 16,027
Recognized Expert Moderator MVP
At the moment, your regular expression checks for: - 0 or more digits (0-9)
- followed by 0 or 1 dots (there may not be a dot)
- followed by 1 or more digits
So, it would match 0, 5, 0.2, .4, 5.5, etc.
pbmods 5,821
Recognized Expert Expert
Heya, baskarpr.
Checking for a non-negative is easy: -
val = parseFloat(str);
-
if(val < 0)
-
{
-
alert('Must not be negative.');
-
}
-
To check to make sure it has a decimal value, simply do this: -
if(val == parseInt(val))
-
{
-
alert('Must have a decimal part.');
-
}
-
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. -
var reg2 = /^\d*\.{0,1}\d+$/;
-
if(!reg2.test(str)) {
-
alert("Not valid");
-
}
1. Is this enough to check for non negative numbers ?
2. How can I check for non negative decimal values ?
Heya, baskarpr.
Checking for a non-negative is easy: -
val = parseFloat(str);
-
if(val < 0)
-
{
-
alert('Must not be negative.');
-
}
-
To check to make sure it has a decimal value, simply do this: -
if(val == parseInt(val))
-
{
-
alert('Must have a decimal part.');
-
}
-
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. :-)
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 :)
pbmods 5,821
Recognized Expert Expert
Merged duplicate threads.
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... :-)
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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
|
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?
|
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...
| |
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:
|
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)\.$
|
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:
|
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...
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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();...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |