SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Uruguay
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with regex

    Hi, I have some problem with a regex
    The next regex returns true while the values are:

    0.1, 50.1, 100, 70, 1500, or 0.1%, 50.1%, 100%, 70%

    But it doens't allow values like 101% or bigger than 100 when we define "%" ( and this is right )

    Now I want to include negative procentage values: -100%,-15%,etc, but not negative numeric values: -0.1, -50.1, -100,etc, These should be invalid

    Code:
    var re = /^(\d+(\.\d+)?|\d{1,2}(\.\d+)?%|100(\.0+)?%)$/;
    Thanks in advance

  2. #2
    SitePoint Member jmiletic's Avatar
    Join Date
    Oct 2008
    Location
    Ohio
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    change to:

    var re = /^(\d+(\.\d+)?|\d{1,2}(\.\d+)?%|100(\.0+)?%)|-(\d{1,2}(\.\d+)?%|100(\.0+)?%)$/;

    The only thing is that on the positive side it allows a number like 1500....

    var re = /^(\d{1,2}(\.\d+)?%|100(\.0+)?%)|-(\d{1,2}(\.\d+)?%|100(\.0+)?%)$/;

    this will give you only percentages...
    Joyce Miletic RHCT + Systems Engineer
    Navisite Dedicated Hosting
    http://dedicatedhosting.navisite.com
    "24/7/365" Myth or Reality?

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ratamaster View Post
    Code:
    var re = /^(\d+(\.\d+)?|\d{1,2}(\.\d+)?%|100(\.0+)?%)$/;
    I would change your 1st regex to

    /\d{1,2}(\.\d+)?%|100(\.0+)?%|\d+(\.\d+)?/

    The loose conditions should be put behind. Add a negative symbol using (-)?

    ^(-)?\d{1,2}(\.)?(\d+)?%$|^100(\.0+)?%$|^\d+(\.\d+)?$

    Maybe you need to check where is ^ or $ be used.


Bookmarks

Posting Permissions

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