Solved

Regular expression for negative float

Posted on 2003-11-03
11
954 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

830 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