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 4787 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...
|
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...
|
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}...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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....
|
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,...
| |
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: 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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |