Solved

Regular expression for negative float

Posted on 2003-11-03
11
938 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now