SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expressions && Number is in Range question

    I have this code:

    var Validate = (function() {

    var numberPattern = new RegExp(/^[0-9]+$/)
    ,decimalNumberPattern = new RegExp(/^-?\d+(\.\d+)?$/);

    return {
    Number: function(numberToValidate, allowDecimals)
    {
    ///<returns type="Boolean" />
    ///<usage>Validate.Number(5) - number</usage>
    ///<usage>Validate.Number(0.5, true) - decimal number</usage>

    if (!numberToValidate) {alert('numberToValidate is missing');}
    var tempNumber = '"'+numberToValidate+'"';
    if (allowDecimals) {
    return (!isNaN(numberToValidate) &&
    (tempNumber.indexOf(".") >= 0 || tempNumber.indexOf(",") >= 0) &&
    decimalNumberPattern.test(numberToValidate));
    }
    else {
    return (!isNaN(numberToValidate) &&
    (!tempNumber.indexOf(".") >= 0 || tempNumber.indexOf(",") >= 0) &&
    numberPattern.test(numberToValidate));
    }
    },

    Range: function(numberToValidate, lowerBound, upperBound, allowDecimals)
    {
    ///<returns type="Boolean" />
    ///<usage>Validate.Range(0.3, 0.5, 4, true);</usage>
    ///<usage>Validate.Range(3, 0, 4, false);</usage>

    if (!numberToValidate) {alert('numberToValidate is missing');return false;}
    if (!lowerBound) {alert('lowerBound is missing');return false;}
    if (!upperBound) {alert('upperBound is missing');return false;}

    if (allowDecimals) {
    if (!Validate.Number(numberToValidate, true)) {
    alert('The argument "numberToValidate" is not a valid decimal number!');
    return false;
    }
    }
    else {
    if (!Validate.Number(numberToValidate)) {
    alert('The argument "numberToValidate" is not a valid number!');
    return false;
    }
    }
    return (numberToValidate > lowerBound) && (numberToValidate < upperBound);
    }
    };
    })();


    So, now if I want to validate a number I could write:
    Code:
    alert(Validate.Number(4)); // Outputs: true - 4 is a number
    and for a decimal number:
    Code:
    alert(Validate.Number(0.4, true)); // Outputs: true - 0.4 is decimal
    But when I try to check whether a number is in a range of two given values I keep getting an error: lowerBound is missing...

    Code:
    var num = 2 , lower = 0 , upper = 5;
    alert(Validate.Range(num, lower, upper)); // Outputs: lowerBound is missing !!!
    Q1:
    Why am I getting this error when I do specify all arguments??

    Q2:
    For decimal number validation I use this regex:
    Code:
    decimalNumberPattern = new RegExp(/^-?\d+(\.\d+)?$/);
    But it checks only for the .(dot); I would like it to check for ,(comma) too. How to change the regexp so I can check for comma too?


    TIA for any answers.
    Proud member of this community.
    ____________________________

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by haiku View Post
    Code:
    if (!lowerBound)
    How will that evaluate if lowerbound is passed 0?
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    0 is false... (damn)

    so it should be passed as string, right?

    Edit: just checked using the lowerBound as '0' and works. cool.
    Proud member of this community.
    ____________________________

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by haiku View Post
    0 is false... (damn)

    so it should be passed as string, right?

    Edit: just checked using the lowerBound as '0' and works. cool.
    It doesn't have to be a string; to check its presence you could use
    Code:
    if( lowerBound==null )
    I would have allowDecimals as the second parameter, that way it's usable without a range being specified.
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    point noted! thanks
    Proud member of this community.
    ____________________________

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Genrally, you don't want to check function arguments in javascript, because it is a dynamic language and works on the GIGO principle. Besides that, each validation method should only check its own constraints, e.g. Range shouldn't check if object is a valid number or has a decimal point, because this is what other methods are for.

    Regarding number regular expression, it can be like this:
    Code:
    number_regexp = /^-?(\d+|(\d*[.,]\d+))$/
    This allows "5", "-5", "5,5" and ",5".

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean here, (though, I don't really know what GIGO principle means...), and you're perfectly right! I'll try to polish my code a little bit.

    Great work with that regex too! Much appreciated!

    Danke schon!
    Proud member of this community.
    ____________________________

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bitte sehr!

    GIGO - http://en.wikipedia.org/wiki/Garbage_in,_garbage_out

    Applied to functions, this basically means that bad params should cause bad results. Your function should not (and cannot) filter out all kinds of nonsense that might be passed to it.

  9. #9
    SitePoint Zealot
    Join Date
    Apr 2008
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great link, I've found this funny enough to make me smile:
    Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?
    thumbs up!
    Proud member of this community.
    ____________________________


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
  •