Regular expression for negative float

Hi Guys,
   Need a regular expression for  floating point numbers, should allow negative numbers as well.
Regards,
Ras
rasarajaAsked:
Who is Participating?
 
NetGrooveCommented:
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
 
NetGrooveCommented:
write some examples of formats you would like to accept.
Do you need only true or false with an alert in false case?
0
 
NetGrooveCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
James RodgersWeb Applications DeveloperCommented:
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
 
rasarajaAuthor Commented:
Jester/NetGroove,
  what is the ^ for ? What is it negating ?
0
 
rasarajaAuthor Commented:
Oops ! Sorry ..it is for begining .. that's fine..
0
 
rasarajaAuthor Commented:
Ooops ! Sorry ..got it ..for the begining ...
0
 
rasarajaAuthor Commented:
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
 
James RodgersWeb Applications DeveloperCommented:
change it to

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

0
 
James RodgersWeb Applications DeveloperCommented:
thanks for the points
0
 
NetGrooveCommented:
Regular grading is an A.
B is only in the school when you are the teacher.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.