Friday, November 12, 2010

Java Script Regular Expression to restrict repetitive characters in a string

Following java script regular expression will restrict the numbers that contains 4 repetitive digits in a number.


function verifyAccessCode(){
    var val = document.forms[0].textbox.value;
    var exp1 = new RegExp("1111");
    var exp2 = new RegExp("2222");
    var exp3 = new RegExp("3333");
    var exp4 = new RegExp("4444");
    var exp5 = new RegExp("5555");
    var exp6 = new RegExp("6666");
    var exp7 = new RegExp("7777");
    var exp8 = new RegExp("8888");
    var exp9 = new RegExp("9999");
    var exp10 = new RegExp("0000");
    if(exp1.test(val) || exp2.test(val) || exp3.test(val) || exp4.test(val) || exp5.test(val)
        || exp6.test(val) || exp7.test(val) || exp8.test(val) || exp9.test(val) || exp10.test(val)){
        return false;
    }
    return true;
}

No comments:

Post a Comment