Solved

Regular expression for negative float

Posted on 2003-11-03
11
960 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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…
Suggested Courses

623 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