Solved

Regular expression for negative float

Posted on 2003-11-03
11
942 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

867 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

23 Experts available now in Live!

Get 1:1 Help Now