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
Need a regular expression for floating point numbers, should allow negative numbers as well.
Regards,
Ras
Here a simple check:
<script>
function isFloat(theValue){
return (/^-?\d+(\.\d+)?$/).test(t heValue+'' );
}
alert(isFloat(1));
alert(isFloat(-100));
alert(isFloat(12.5));
alert(isFloat('1F'));
</script>
<script>
function isFloat(theValue){
return (/^-?\d+(\.\d+)?$/).test(t
}
alert(isFloat(1));
alert(isFloat(-100));
alert(isFloat(12.5));
alert(isFloat('1F'));
</script>
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.te stForm.myF loat.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>
<!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.te
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>
ASKER
Jester/NetGroove,
what is the ^ for ? What is it negating ?
what is the ^ for ? What is it negating ?
ASKER
Oops ! Sorry ..it is for begining .. that's fine..
ASKER
Ooops ! Sorry ..got it ..for the begining ...
ASKER
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)+/
It is taking
9.a
9.bbbbb
Anything !!
Maybe we can do something like this - /^ - \d+(\.)?(\d)+/
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
thanks for the points
Regular grading is an A.
B is only in the school when you are the teacher.
B is only in the school when you are the teacher.
Do you need only true or false with an alert in false case?