...

View Full Version : Float validations using Regular Expression



prajwala
02-20-2007, 04:33 AM
can u please help me out in giving validations for positive floating point numbers.
i have used regular expression for positive as well as negative numbers.now i want exxpression for positive float numbers
var regexp=/^([+/-]?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+\-]?[0-9]+)?))$/;
:)

gmn17
02-21-2007, 08:32 AM
regexp is deprecated, and for good reason.

liorean
02-21-2007, 01:48 PM
prajwala:
This simple change should make it match only positive floats:
/^(\+?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))$/

By the way, your original regex has an error - [+/-] matches a '+', a '/' or a '-'. But '/' isn't a sign, so it's not legal there. Use [+-] instead.

You have a similar syntax in the exponent part, here being [+\-]. That backslash is totally redundant and can be removed --> [+-]
regexp is deprecated, and for good reason.Absolute bull****. Regex aren't in any way, shape or form deprecated.

floryncfd
10-03-2008, 03:27 PM
This ^([+/-]?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+\-]?[0-9]+)?))$
did not work for me (MySQL 5.0.45). For example the string '-2.14g846' is validated as a float number, but it shouldn't be as there is a 'g' in the string.

But I found another regexp:
'^([[+-]?((([[:digit:]]+[[.period.]]?)|([[:digit:]]*[[.period.]][[:digit:]]+))([eE][+-]?[[:digit:]]+)?))$'

For more details check http://dev.mysql.com/doc/refman/5.0/en/regexp.html

rnd me
10-03-2008, 06:46 PM
str="1323.343243"
if( str === (str * 1) ){ alert("valid number"); }

liorean
10-03-2008, 08:40 PM
This ^([+/-]?((([0-9]+(\.)?)|([0-9]*\.[0-9]+))([eE][+\-]?[0-9]+)?))$
did not work for me (MySQL 5.0.45). For example the string '-2.14g846' is validated as a float number, but it shouldn't be as there is a 'g' in the string.

But I found another regexp:
'^([[+-]?((([[:digit:]]+[[.period.]]?)|([[:digit:]]*[[.period.]][[:digit:]]+))([eE][+-]?[[:digit:]]+)?))$'

For more details check http://dev.mysql.com/doc/refman/5.0/en/regexp.htmlThis is not a MySQL forum. This is a JavaScript forum, and the regex you posted does not work in the JavaScript regex syntax. The original regex had the problems I stated above, fixing those provides a working JavaScript regex.

You can try this in any browser:
javascript:/^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/.test('-2.14g846')As you see, the result is false.

floryncfd
10-09-2008, 07:34 AM
This is not a MySQL forum. This is a JavaScript forum, and the regex you posted does not work in the JavaScript regex syntax. The original regex had the problems I stated above, fixing those provides a working JavaScript regex.

You can try this in any browser:
javascript:/^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/.test('-2.14g846')As you see, the result is false.

Sorry for my mistake. Thanks liorean for your remark



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum