Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    77
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question Float validations using Regular Expression

    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]+)?))$/;

  • #2
    New Coder
    Join Date
    May 2005
    Location
    Los Angeles
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    regexp is deprecated, and for good reason.

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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 --> [+-]
    Quote Originally Posted by gmn17 View Post
    regexp is deprecated, and for good reason.
    Absolute bull****. Regex aren't in any way, shape or form deprecated.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Location
    Baia Mare, Romania
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    Last edited by floryncfd; 10-03-2008 at 02:30 PM.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,190
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Code:
    str="1323.343243"
    if( str === (str * 1) ){ alert("valid number"); }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by floryncfd View Post
    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
    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:
    Code:
    javascript:/^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/.test('-2.14g846')
    As you see, the result is false.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • Users who have thanked liorean for this post:

    floryncfd (10-09-2008)

  • #7
    New to the CF scene
    Join Date
    Oct 2008
    Location
    Baia Mare, Romania
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by liorean View Post
    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:
    Code:
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •