js validators

In this example below you will see how to do a js validators with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by kaktys, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright kaktys ©
  • HTML
  • CSS
  • JavaScript
    

/*Downloaded from https://www.codeseek.co/kaktys/js-validators-mRXLMb */
    


/*Downloaded from https://www.codeseek.co/kaktys/js-validators-mRXLMb */
    








//############ VALIDATORS ####################################

function advanced_string_validation(str) {
    //cheks for 3 same later on the row

    for (i = 0; i < str.length; i++) {
        if (str[i] == str[i + 1]) {
            if (str[i] == str[i + 2]) {
                return false;
            }
        }
    }
    return true;
}

function sin_luhn(str, err_text) {
    //Canadian SIN validator
    var str = str.match(/\d/g).join("");

    var luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
    var counter = 0;
    var incNum;
    var odd = false;
    var temp = String(str).replace(/[^\d]/g, "");
    if (temp.length == 0) return false;
    for (var i = temp.length - 1; i >= 0; --i) {
        incNum = parseInt(temp.charAt(i), 10);
        counter += (odd = !odd) ? incNum : luhnArr[incNum];
    }
    return counter % 10 == 0;
}

function cc_luhn(str, err_text) {
    //CC validator
    var str = str.match(/\d/g).join("");

    var valid =
        str.split("").reduceRight(function(prev, curr, idx) {
            prev = parseInt(prev, 10);
            if ((idx + 1) % 2 !== 0) {
                curr = (curr * 2).toString().split("").reduce(function(p, c) {
                    return parseInt(p, 10) + parseInt(c, 10);
                });
            }
            return prev + parseInt(curr, 10);
        }) %
        10 ===
        0;
    if (!valid) {
        err_text.push("Card number is not valid");
    }

    return valid;
}

function dob_luhn(str, err_text) {
    //DOB input text validation
    //must be separated by /

    var valid = true;
    var d = new Date();
    var yearToday = d.getFullYear();

    var the_day = str.split("/")[0];
    var the_month = str.split("/")[1];
    var the_year = str.split("/")[2];

    if (the_day > 31) {
        valid = false;
    }
    if (the_month > 12) {
        valid = false;
    }
    if (the_year > yearToday) {
        valid = false;
    }
    if (the_year < yearToday - 100) {
        valid = false;
    }
    if (!valid) {
        err_text.push("Date of birth is not valid");
    }
    return valid;
}

function exp_luhn(str, err_text) {
    //EXP input text validation
    //must be separated by /

    var valid = true;
    var d = new Date();
    var yearToday = d.getFullYear();

    var the_month = str.split("/")[0];
    var the_year = "20" + str.split("/")[1];

    if (the_month > 12) {
        valid = false;
    }
    if (the_year < yearToday) {
        valid = false;
    }
    if (the_year > yearToday + 10) {
        valid = false;
    }
    if (!valid) {
        err_text.push("Card expiration date is not valid");
    }
    return valid;
}








var qasame__ = function(err_text) {
    var res = true;
    if (!valid_a(err_text)) {
        res = false;
    }

    if (!valid_q(err_text)) {
        res = false;
    }
    return res;
}





// validate answer for same value

function valid_a(err_text) {
    var arr = $(".answers")
        .map(function() {
            return $(this).val();
        })
        .get();
    if ($.unique(arr).length != $(".answers").length) {
        err_text.push(
            "You can not choose two identical answers. (Error # AE1007-3)"
        );
        return false;
    }
    return true;
}

// validate question for same value

function valid_q(err_text) {
    var arr = $(".questions_")
        .map(function() {
            return $(this).val();
        })
        .get();
    if ($.unique(arr).length != $(".questions_").length) {
        err_text.push(
            "You can not choose two identical questions. (Error # AE1007-3)"
        );
        return false;
    }
    return true;
}

Comments