Solved

Regular expression for negative float

Posted on 2003-11-03
11
950 Views
Last Modified: 2012-06-21
Hi Guys,
   Need a regular expression for  floating point numbers, should allow negative numbers as well.
Regards,
Ras
0
Comment
Question by:rasaraja
  • 4
  • 4
  • 3
11 Comments
 
LVL 10

Expert Comment

by:NetGroove
ID: 9671144
write some examples of formats you would like to accept.
Do you need only true or false with an alert in false case?
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9671321
Here a simple check:

<script>

function isFloat(theValue){
  return (/^-?\d+(\.\d+)?$/).test(theValue+'');
}

alert(isFloat(1));
alert(isFloat(-100));
alert(isFloat(12.5));
alert(isFloat('1F'));
</script>


0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9671348
try this to see if it fits your needs

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
      
<script>
      function testFloat(){
      regEx=/^-?\d*(\.\d*)?/
            if(!regEx.test(document.testForm.myFloat.value)){
                  alert("not valid");
            }
            return false;
      }
</script>
</head>

<body>
<form name="testForm" onSubmit="return testFloat()">
<input type="text" name="myFloat" value="">

<input type="submit" name="go" value="go">

</form>


</body>
</html>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:rasaraja
ID: 9671683
Jester/NetGroove,
  what is the ^ for ? What is it negating ?
0
 

Author Comment

by:rasaraja
ID: 9671698
Oops ! Sorry ..it is for begining .. that's fine..
0
 

Author Comment

by:rasaraja
ID: 9671740
Ooops ! Sorry ..got it ..for the begining ...
0
 

Author Comment

by:rasaraja
ID: 9671903
Jester/NetGroove, there's a problem ... having (\.\d+)? is accepting anything after a dot !

It is taking
9.a
9.bbbbb

Anything !!

Maybe we can do something like this - /^ - \d+(\.)?(\d)+/
0
 
LVL 25

Assisted Solution

by:James Rodgers
James Rodgers earned 50 total points
ID: 9672129
change it to

regEx=/^-?\d*(\.\d*)?$/

0
 
LVL 10

Accepted Solution

by:
NetGroove earned 50 total points
ID: 9672156
That is already there:

<script>

function isFloat(theValue){
  return (/^-?\d+(\.\d+)?$/).test(theValue+'');
}

alert('1: '+isFloat(1));
alert('-100: '+isFloat(-100));
alert('12.5: '+isFloat(12.5));
alert('9.bbbbb: '+isFloat('9.bbbbb'));
</script>


The ^ is for string beginning.
The ? is a quatifier requestin maximum of one or none.
Imprtant is the $ at the end to request the end of the string. Otherwise you could realy add anything after the match.


0
 
LVL 25

Expert Comment

by:James Rodgers
ID: 9717727
thanks for the points
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9718184
Regular grading is an A.
B is only in the school when you are the teacher.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Yet another array challenge. This time utilizing plupload image sorting from server. 17 55
add a 3rd selection to sum 7 29
jqury 17 30
Button and js nou working 3 8
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question