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 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2006
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular expression to except single dot ..?

    hii

    <input type="text" id="txt" maxlength="9" onkeypress="return numbersonly(event);">

    onkeypress i am calling a function numbersonly(), in this function i need a regular expression which excepts a single dot not more than one dot.
    once the user enters one dot, user can enter maximum of 2 numbers after dot
    ex:
    .11
    and dot should not be allowed if user enters more than 6 numbers
    ex: 1111111. this should not be allowed.
    ex:111111.11 this is allowed

    some examples
    .11
    11.11
    111.11
    1111.11
    11111.11
    111111.11

    can anyone give a RE for this...

    thnx
    Murali.

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    [0-9]+(\.[0-9][0-9])?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by nikkiH View Post
    [0-9]+(\.[0-9][0-9])?
    That insists on two digits after any decimal, whereas I understand the requirement for a maximum of two. Also it needs start and end delimiters (^ $) or it will pass anything containing a digit.

    If one digit is allowed after the decimal:
    Code:
    /^\d+(\.\d{1,2})?$/
    should get the assignment completed.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by Arty Effem View Post
    If one digit is allowed after the decimal:
    Code:
    /^\d+(\.\d{1,2})?$/
    should get the assignment completed.
    No - "and dot should not be allowed if user enters more than 6 numbers"

    So:-

    /^\d{0,6}(\.\d{1,2})?$/

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,778
    Thanks
    19
    Thanked 155 Times in 146 Posts
    Quote Originally Posted by Philip M View Post
    No - "and dot should not be allowed if user enters more than 6 numbers"

    So:-

    /^\d{0,6}(\.\d{1,2})?$/
    your regex doesn;t appear to allow integers that are greater than 6 digits long, i.e.: 88888888 (which appears to be perfectly valid for his uses).

    you need some kind of conditional in there that says effectively: "allow a decimal point and up to 2 decimal places in the number only if the number is less than or equal to 999999"
    Last edited by chump2877; 02-06-2007 at 07:03 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    /^(\d{0,6}\.\d{1,2})|(\d+)$/
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,037
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    Quote Originally Posted by chump2877 View Post
    your regex doesn;t appear to allow integers that are greater than 6 digits long, i.e.: 88888888 (which appears to be perfectly valid for his uses).

    you need some kind of conditional in there that says effectively: "allow a decimal point and 2 decimal places in the number only if the number is less than or equal to 999999"
    Yep, you are right.
    So two tests are needed:-

    /^d{7,}$/ // OK if 7 or more digits and nothing but
    /^\d{0,6}(\.\d{1,2})?$/ // OK if up to six digits followed by one or two decimal digits.

    Felgall has got it right in one expression.
    Last edited by Philip M; 02-06-2007 at 07:08 PM.

  • #8
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by Philip M View Post
    Yep, you are right.
    So two tests are needed:-

    /^d{7,}$/ // OK if 7 or more digits and nothing but
    /^\d{0,6}(\.\d{1,2})?$/ // OK if up to six digits followed by one or two decimal digits.

    Felgall has got it right in one expression.
    I'm afraid it doesn't meet the spec under my tests.

    I suggest: /(^\d{0,6}(\.\d{1,2})?$)|^\d{7,}$/

  • #9
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Arty Effem View Post
    I'm afraid it doesn't meet the spec under my tests.

    I suggest: /(^\d{0,6}(\.\d{1,2})?$)|^\d{7,}$/
    That is just a slightly longer variant of mine since the first part defines the decimal as optional for numbers up to 999999 and those numbers would be caught by the second test in my version so that making it optional in the first test is then unnecessary.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #10
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by felgall View Post
    That is just a slightly longer variant of mine since the first part defines the decimal as optional for numbers up to 999999 and those numbers would be caught by the second test in my version so that making it optional in the first test is then unnecessary.
    In your expression /^(\d{0,6}\.\d{1,2})|(\d+)$/ , ^ and $ have precedence over | , so effectively it evaluates to "anything ending with a digit".

    It works if you write it: /^((\d{0,6}\.\d{1,2})|(\d+))$/

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Arty Effem View Post
    In your expression /^(\d{0,6}\.\d{1,2})|(\d+)$/ , ^ and $ have precedence over | , so effectively it evaluates to "anything ending with a digit".

    It works if you write it: /^((\d{0,6}\.\d{1,2})|(\d+))$/

    I was sure those extra parentheses were there when I typed it in to start with. Maybe I just imagined they were there. Certainly they should be.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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