Link to home
Start Free TrialLog in
Avatar of rasaraja
rasaraja

asked on

Regular expression for negative float

Hi Guys,
   Need a regular expression for  floating point numbers, should allow negative numbers as well.
Regards,
Ras
Avatar of NetGroove
NetGroove

write some examples of formats you would like to accept.
Do you need only true or false with an alert in false case?
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>


Avatar of James Rodgers
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>
Avatar of rasaraja

ASKER

Jester/NetGroove,
  what is the ^ for ? What is it negating ?
Oops ! Sorry ..it is for begining .. that's fine..
Ooops ! Sorry ..got it ..for the begining ...
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)+/
SOLUTION
Avatar of James Rodgers
James Rodgers
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for the points
Regular grading is an A.
B is only in the school when you are the teacher.