1

Question: Maximum of 5 characters (0.000 < courier weight <=5.000 with max of 3 digits after decimal point) Values can be between 0 and 5. Excluding 0 and including 5.

My codes:

function checkWeightOfParcel(weightOfParcel) {
    var regex=/^[0-5]+\.[0-9]$/;
    if(regex.test(weightOfParcel)) {
        alert('correct!!!')
        return true;
    }
    else {
        alert('wrong regex!')
        return false;
    }
}

*my code only can validate range 0.000 to 5.999, it is wrong. How to remove 0.999 behind to set the max value is 5.000 Any website or forum to learn javascript regex? i'm new to it, thanks for help.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
user3066033
  • 399
  • 1
  • 4
  • 9
  • So it should validate 0.000 - 5.000? Don't think your regex does that at the moment. It would validate 500.0 – putvande Dec 04 '13 at 13:54

5 Answers5

3

No reason to use regex for this. Parse it to a number using parseFloat, and then use simple conditions:

function checkWeightOfParcel(weightOfParcel) {
    //Turn it into a number
    weightOfParcel = parseFloat(weightOfParcel);

    if (weightOfParcel > 0 && weightOfParcel <= 5) {
        alert('correct!!!')
        return true;
    }
    else {
        alert('wrong regex!')
        return false;
    }
}
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • @user3066033 You don't need to per-se. `"0.001" > 0` would return true, while `"0.001a" > 0` returns false. `parseFloat` tries to fix all of the users stupid mistakes, and `parseFloat("0.001a") > 0` **does** return true. Check the [MDN page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) for more examples of what `parseFloat` understands. – h2ooooooo Dec 04 '13 at 14:14
2

You shouldn't be using regex to do this. Simple code check like this is suffice:

if(weightOfParcel > 0 && weightOfParcel <= 5) {
    alert('correct!!!')
    return true;
}
else {
    alert('wrong value!')
    return false;
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • can i know why i shouldn't use regex? i thought it is specific even it is hard...by the way, thanks help! – user3066033 Dec 04 '13 at 14:20
  • @user3066033 Because it's hard and not really needed in this case. A combined solution (regexp to validate the characters used and number comparison) would be much easier. You can take a look at the regexp i came up with (purely as a challenge) and imagine how difficult it would be to maintain code that relied on it. – Tibos Dec 04 '13 at 14:22
  • @Tibos: Thanks for your answer, yes that is precisely regex should be used only when it is needed. – anubhava Dec 04 '13 at 14:25
2

While you shouldn't use regexp for this, here is one that works:

/^(?:(?:[1-4](?:\.\d{1,3})?)|(?:5(?:\.0{1,3})?)|(?:0\.(?:(?:(?:[1-9]\d{0,2}))|(?:0[1-9]\d?)|(?:00[1-9]))))$/

A site that is great for trying out and understanding regex is this one (already filled with your regex): http://regex101.com/r/cM6xC9

Basically, i split your cases in two:

  • Numbers starting with 1,2,3,4 which may be followed by a . followed by 1,2 or 3 decimals.
  • Numbers starting with 5 which may be followed by a . followed by 1,2 or 3 zeroes.
  • Numbers starting with 0 which must be followed by a . followed by
    • non zero digit followed by 0-2 more digits
    • 0 followed by non zero digit followed by 0-1 more digits
    • 00 followed by non zero digit
Tibos
  • 27,507
  • 4
  • 50
  • 64
  • Nice try, but you've forgotten the hardest part, exclude `0.000` :D –  Dec 04 '13 at 14:09
  • For somer reason i thought it was valid. Anyway, i fixed it now. (and the regexp is almost double in size). Don't use regexp for ranges of numbers! – Tibos Dec 04 '13 at 14:17
  • +1 for supplying a regex solution as requested even though it's not the best solution (to use regex, not to use *your* solution). A+ for effort. :) – h2ooooooo Dec 04 '13 at 14:28
  • @h2ooooooo I took it as a challenge and it was a lot easier in version 1, where i matched `0 <= x <= 5`. Eliminating the 0 was painful :). – Tibos Dec 04 '13 at 14:30
  • Have you updated the link? It seems that this one fails : `0.111`. –  Dec 04 '13 at 14:53
1

Another bad idea :D

/^((?!0(\.0{1,3})?$)[0-4](\.\d{1,3})?|5(\.0{1,3})?)$/

http://regex101.com/r/kV5qK0

  • Thank you for this. It solve my case with problem 0..01 enter. Parsefloat still let it go, but this regex wont. how come? Any idea? – user3066033 Dec 05 '13 at 08:49
  • @user3066033 Your question is not understandable. Could you express yourself a little better please? –  Dec 05 '13 at 12:17
  • Sorry for my broken english. What i mean is value 0..01 still can return true with the use of parseFloat, while this regex won't. Why and how does it work. Thank you. – user3066033 Dec 05 '13 at 16:48
  • @user3066033 Do you want either allow or deny `0..01`? –  Dec 06 '13 at 20:32
  • weightOfParcel = parseFloat(weightOfParcel); allowed double dot(0..01) return true value, while regex you provided are not. I deny the use of double dot by user. I just want to know why parseFloat will return true value. thanks! – user3066033 Dec 07 '13 at 03:48
  • @user3066033 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#parseInt_and_parseFloat_functions : "If it encounters a character other than a sign (+ or -), a numeral (0-9), a decimal point, or an exponent, then it returns the value up to that point and ignores that character and all succeeding characters.". Roughly speaking, it means that `parseFloat('0.1a1')` stops at index 3 then returns `0.1`, while `parseFloat('0..11')` stops at index 2 then returns `0.` which is evaluated as `0`. –  Dec 07 '13 at 05:24
  • Thanks! i got it! Do you mind to look at my posted 2nd questions which regarding about javascript too. I use but problem is i can't use javascript to validate it. Here's the link http://stackoverflow.com/questions/20379251/date-leap-year-validation – user3066033 Dec 07 '13 at 15:19
0

Why are you using regex? it is easier to compare numbers.

function checkWeightOfParcel(weightOfParcel) {
    // var weightOfParcel = parseFloat(weightOfParcel);  // to avoid double parsing
    return parseFloat(weightOfParcel) < 0 || parseFloat(weightOfParcel) > 5
}
i100
  • 4,529
  • 1
  • 22
  • 20
  • what means to avoid double parsing? – user3066033 Dec 04 '13 at 14:21
  • parseFloat(weightOfParcel) < 0 || parseFloat(weightOfParcel) parses twice - once comparing for less than zero and second comparing with five. I'd prever to parse it on finction call - checkWeightOfParcel(parseFloat(weightOfParcel)); – i100 Dec 04 '13 at 15:04