SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    1 Decimal Validation

    Dear All,
    I can check now a field is isNumeric via this method var isNumeric = /^[0-9.]+$/;. My problem now user can put two or more decimal and I want to limit just to single decimal and only 2 numbers before the decimal? How to edit please?

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newtomysql View Post
    My problem now user can put two or more decimal and I want to limit just to single decimal and only 2 numbers before the decimal?
    What does this mean? Decimal numbers or decimal points?

    Does it mean 2 or more decimal points or decimal numbers. Are decimal points mandatory or optional. Do you want up to 2 numbers before the decimal point or up to 2 numbers before the decimal point?

    Post a pattern of the format you want and describe which components are mandatory and which are optional otherwise people will have to guess or make assumptions.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Something like the following should do.

    /^[0-9]+(\.[0-9]+)?$/;

    [0-9]+ is for any number of numerals before the decimal point
    (...)? indicates that the contents are to be considered optional
    \. within the optional section is for a fullstop
    [0-9]+ after the fullstop is for any number of numbers following it

    As an extra, of you don't want a back-reference created for the parenthesis part, you can use ?: at the start of the parenthesis content to tell the regex engine not to do so. See the backreferences section for more details on this.

    /^[0-9]+(?:\.[0-9]+)?$/;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Webdev,
    So samples which I want to allow will be 23, 23.1,56.1,67 etc. Meaning that I want only 2 number before the decimal and one number after the decimal. If it is not decimal number then just limit two digits too. Thank you. Hope I am clearer. The minimum will 2 digit numbers and that is mandatory.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Webdev,
    So samples which I want to allow will be 23, 23.1,56.1,67 etc. Meaning that I want only 2 number before the decimal and one number after the decimal. If it is not decimal number then just limit two digits too. Thank you. Hope I am clearer. The minimum will 2 digit numbers and that is mandatory.
    Will it always be two before the decimal point? If so, that would be:
    [0-9]+{2}
    but if it's 1 or 2 allowed before the decimal point, such as with 9.5, then it would be:
    [0-9]+{1,2}

    An updated regular expression to account for this new information, with a mandatory 2 digits before and an optional decimal value of one digit, is:
    /^[0-9]+{2}(\.[0-9])?$/;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Webdev,
    So samples which I want to allow will be 23, 23.1,56.1,67 etc. Meaning that I want only 2 number before the decimal and one number after the decimal. If it is not decimal number then just limit two digits too. Thank you. Hope I am clearer. The minimum will 2 digit numbers and that is mandatory.
    One option:

    Code:
            
          <script type="text/javascript">
    
                function validateNum(num){
                    var regex = /^\d{2}(\.\d)?$/;
                    if(regex.test(num)) {
                        alert('valid');
                    } else {
                        alert('invalid');
                    }
                }
    
            </script>

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Another option, which removed the need for a regular expression completely is:

    Code javascript:
    function validateNum(value){
        var num = Number(value);
        if (num >= 10 && num < 100) {
            alert('valid');
        } else {
            alert('invalid');
        }
    }

    Number(value) converts it to a number, which can include decimal values too.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Another option, which removed the need for a regular expression completely is:

    Code javascript:
    function validateNum(value){
        var num = Number(value);
        if (num >= 10 && num < 100) {
            alert('valid');
        } else {
            alert('invalid');
        }
    }

    Number(value) converts it to a number, which can include decimal values too.
    But 23.12345 alert's 'valid' which is not what the op wants. They specified only 1 decimal place after the decimal point.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by webdev1958 View Post
    But the 23.12345 alert's 'valid' which is not what the op wants. They specified only 1 decimal place after the decimal.
    Ahh yes, that would involve another check for something like:
    Math.floor(num * 10) === num * 10
    which can serve to complicate things.

    Code javascript:
    function validateNum(value){
        var num = Number(value);
        if (num >= 10 && num < 100 && Math.floor(num * 10) === num * 10) {
            alert('valid');
        } else {
            alert('invalid');
        }
    }

    So how can things be made less complicated. By off-loading some of the work to other functions?

    Code javascript:
    function isTwoDigitNumber(num) {
        return num >= 10 && num < 100;
    }
    function decimalPlaces(num) {
        return num.toString().split('.')[1].length;
    }
    function validateNum(value){
        var num = Number(value);
        if (TwoDigitNumber(num) && decimalPlaces(num) <= 1) {
            alert('valid');
        } else {
            alert('invalid');
        }
    }

    The code is a lot easier to read and understand that way, but a regular expression might be preferred.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    The code is a lot easier to read and understand that way, but a regular expression might be preferred.
    I guess whether it is easier to read or not boils down to personal preference. For me, the less code I have to read the easier it is.

    The only thing the op didn't specify is if something like 00.1 or 08 is allowed. If it isn't then it's only a minor tweak in the regex

    Code:
    function validateNum(num){
                    var regex = /^[1-9][0-9](\.\d)?$/;
                    if(regex.test(num)) {
                        alert('valid');
                    } else {
                        alert('invalid');
                    }
                }
    Personally I find the regex code easier to read.

  11. #11
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear All,
    I have tried both the method var isDecimal = /^[1-9][0-9](\.\d)?$/; and /^[0-9]+{2}(\.[0-9])?$/;. I get an error invalid quantifier at ^. I am hopefully clear again I would accept 2.3, 23.3,45.2,12.4. I can accept one or two number before decimal but after decimal only 1 single number.

  12. #12
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear All,
    I have tried both the method var isDecimal = /^[1-9][0-9](\.\d)?$/; and /^[0-9]+{2}(\.[0-9])?$/;. I get an error invalid quantifier at ^. I am hopefully clear again I would accept 2.3, 23.3,45.2,12.4. I can accept one or two number before decimal but after decimal only 1 single number.
    I'm not sure how you expect anyone to tell you what you have done wrong if you don't post your code. Look at the demo code I posted. It works without problems in my IE9 and FF8.

    Also, earlier you said

    The minimum will 2 digit numbers and that is mandatory
    and now you say

    I can accept one or two number before decimal....
    It appears to me either you don't know what you are doing or you are making this up as you go along. If you now accept 1 or 2 digits before the decimal then it's only a very minor change to the code I posted. You should be able to figure it out for yourself. I don't do spoon feeding.

    I won't be spending anymore time on this just in case you make more things up

  13. #13
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Webdev,
    Ok I manage to solve my problem and here is the final solution as benefit for the rest too. /^[0-9]{1,2}(\.[0-9])?$/ This regex works fine thank you for the time and courage and also to Paul for his ideas too.


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
  •