Codemator
Codemator

Reputation: 513

Regular expression check for numbers with a series of patterns

I have a condition where in i need to omit series of numbers with following pattern using javascript.

If 4 digit number

  1. Only numbers and length should be 4
  2. Atleast 3 distinct digits (Eg: of non allowed digits: 1113, 4443)
  3. No 3 consecutive digits both in case of ascending and descending series (Eg: of not allowed digits: 1231, 4321)

If 5-6 digit number

  1. Only numbers and length should be 5-6
  2. Atleast 4 distinct digits (Eg: of non allowed digits: 11113, 44443)
  3. No 3 consecutive digits both in case of ascending and descending series (Eg: of not allowed digits: 12341, 43211)

As far as I am aware you cannot check the ascending and descending number, you can only listen to them like

If 4 digit

^[0-9]{4}$ AND NOT IN
(?:012|210|123|321|234|432|345|543|456|654|567|765|678|876|789|987|111|222|333|444)

If 5-6 digit

^[0-9]{5,6}$ AND NOT IN
(?:0123|3210|1234|4321|2345|5432|3456|6543|4567|7654|5678|8765|6789|9876|1111|2222|3333|4444|5555|6666)

I dont know how to fill "AND NOT IN" in regular expression. Is there a better way?

Upvotes: 1

Views: 2084

Answers (2)

jfriend00
jfriend00

Reputation: 708046

I think the consecutive digits test is much better to do in javascript. Here's a function that tests for any sequence of consecutive digits up or down. You pass how many you want to test for:

function checkConsecutiveChars(str, limit) {
    var lastDigit = str.charCodeAt(0), num = 1, val, delta;
    for (var i = 1; i < str.length; i++) {
        val = str.charCodeAt(i);
        ++num;
        if (num === 2) {
            // calc delta and remember it
            delta = val - lastDigit;
            // see if we have a two char sequence now
            if (Math.abs(delta) !== 1) {
                // not sequential, start over
                num = 1;
            }
        } else {
            // see if consecutive sequence continues and exceeds limit
            if (val === (lastDigit + delta)) {
                if (num >= limit) {
                    return(false);
                }
            } else {
                // sequence stopped
                num = 1;
            }
        }
        lastDigit = val;
    }
    return(true);
}

Working example: http://jsfiddle.net/jfriend00/Nym8Y/


You can check for how many unique digits there are with this function:

function checkDistinctDigits(str, minDistinct) {
    var digits = new Array(10);
    var uniqueCnt = 0, val;
    for (var i = 0; i < str.length; i++) {
        val = str.charCodeAt(i);
        if (val >= 48 && val <= 57) {
            if (!digits[val - 48]) {
                ++ uniqueCnt;
                digits[val - 48] = true;
            }
        }
    }
    return(uniqueCnt >= minDistinct);
}

Working example: http://jsfiddle.net/jfriend00/cpXnW/

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

This kind of thing seems too complex for a regex, since even if you could write it, would you understand it without a bunch of comments?

Instead, try breaking it down:

// assuming var number as string;
var i, l = number.length, prev = -999, dist, prevdist = 0,
    digits = [0,0,0,0,0,0,0,0,0,0], dcnt = 0;
main:
switch(l) {
case 4:
case 5:
case 6:
    for( i=0; i<l; i++) {
        digits[number[i]]++;
        dist = prev-number[i];
        if( prevdist == dist && Math.abs(dist) == 1) {
            alert("Three consecutive digits ("+number.substr(i-2,3)+")");
            break main;
        }
        prev = number[i];
        prevdist = dist;
    }
    for( i=0; i<10; i++) {
        if( digits[i]) dcnt++;
    }
    if( dcnt < (l == 4 ? 3 : 4)) {
        alert("Need at least "+(l == 4 ? 3 : 4)+" distinct digits");
        break main;
    }
    alert("Valid");
    break main;
default:
    alert("Length must be between 3 and 5");
    break main;
}

Upvotes: 2

Related Questions