SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist NokX's Avatar
    Join Date
    Feb 2003
    Location
    Knoxville, TN
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression number range

    is there a way to do a regular expression so that the number being checked is within a certain range?

    for example, i wanna validate an IP address. so the first number needs to be between 1 and 255, and the rest need to be between 0 and 255.

    right now i have this, but it doesn't restrict the range:

    Code JavaScript:
    var ip = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
    if (ip.test(value)) { return true; }

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i've not used it myself but i reckon RegExp.exec() could help with this.

    in the js;definitive guide o'reilly book there's this example code for it:

    Code:
    var pattern = /\bJava\w*\b/g;
    var text = "JavaScript is more fun than Java or JavaBeans!";
    var result;
    while((result = pattern.exec(text)) != null) {
        alert("Matched `" + result[0] + "' at position " + result.index);
    }
    i haven't thought it through in deail but i reckon instead of an alert function do a function which checks the value is less than etc.

  3. #3
    SitePoint Enthusiast WMX's Avatar
    Join Date
    Sep 2008
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about something like this?
    Code:
    var is_valid_ip = function(ip)
    {
        if (ip)
        {
            var octets = ip.split('.');
            if (octets.length == 4)
            {
                var i;
                var octet = 0;
                for (i = 0; i < 4; ++i)
                {
                    octet = parseInt(octets[i], 10);
                    if ((i == 0 && (octet < 1 || octet > 255)) || (i > 1 && (octet < 0 || octet > 255)))
                    {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    };
    But you might also want to check for valid IPs that are defined as private (10.x.x.x, 192.168.x.x etc)

    EDIT: Changed my is_valid_ip function There are probably better ways of doing this, but it works.
    Last edited by WMX; Sep 11, 2008 at 08:29.
    ~

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var rx= /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/;
    //test
    var value1='11.11.111.111';
    var value2='11.111.111';
    alert(value1+'='+rx.test(value1)+'\n'+value2+'='+rx.test(value2))


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
  •