JQuery validator 通用验证

$(document).ready(function () {



    /**

     * validator the username length

     */

    $.validator.addMethod("nameType", function (value, element, params) {

        var regex = /^[A-Za-z0-9]{1,20}$/;

        return regex.test(value);

    }, "Please enter a valid name, length is between 1 to 20.");



    /**

     * validator the Description

     */

    $.validator.addMethod("descType", function (value, element, params) {

        //var regex = /^[A-Za-z0-9]{1,255}$/;

        if(value.length < 255){

            return true;

        }

        return false;

    }, "Please enter a valid description, length is between 1 to 255");



    /**

     * validator the script type

     */

    $.validator.addMethod("scriptType", function (value, element, params) {

        var regex = /^[A-Za-z0-9\s\S]{1,255}$/;

        return regex.test(value);

    }, "Please enter a valid script, length is less than 16384");



    /**

     * validator the ipv4 type

     */

    $.validator.addMethod("IPV4Type", function (value, element, params) {

        var regex = /^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}$/;

        return regex.test(value);

    }, "Please enter a valid IP, Your choice is IPV4.");



    /**

     * validator the ipv6 type

     */

    $.validator.addMethod("IPV6Type", function (value, element, params) {

        var regex = /^\s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$/;

        return regex.test(value);

    }, "Please enter a valid IP, Your choice is IPV6.");



    /**

     * validator the tag's key and value length

     */

    $.validator.addMethod("tagType", function (value, element, params) {

        var regex = /^[A-Za-z0-9]{1,253}$/;

        if (regex.test(value)) {

            var totalLength = 0;

            for (var key in params[0]) {

                totalLength = totalLength + $(params[0][key]).val().length;

            }

            return totalLength <= 254;

        }

        return regex.test(value);

    }, "Please enter a valid tag, Key and Value is less than 254.");



    /**

     * validator the port range

     */

    $.validator.addMethod("portType", function (value, element, params) {

        var number = parseInt(value);

        if (isNaN(number)) {

            return false;

        }

        return number >= 1 && number <= 65535;


    }, "Please enter a valid port, 1 to 65535 and only be number.");



    /**

     * validator the password

     */

    $.validator.addMethod("passwordType", function (value, element, params) {

        var regex = /^[A-Za-z0-9]{6,20}$/;

        return regex.test(value);

    }, "Please enter a password of length between 6 and 20 characters using letter and number.");



    /**

     * validator the disk min size

     */

    $.validator.addMethod("minDisk", function (value, element, params) {

        var regex = /^\d{0,4}$/;

        if (regex.test(value) && value < 1024) {

            return true;

        }

        return false;

    }, "Please enter a valid number");



    /**

     * validator the ram min size

     */

    $.validator.addMethod("minRam", function (value, element, params) {

        var regex = /^\d{0,4}$/;

        if (regex.test(value) && value < 1024) {

            return true;

        }

        return false;

    }, "Please enter a valid number");



    /**

     * validator the disk url type

     */

    $.validator.addMethod("urlType", function (value, element, params) {

        var regex = /^(http|https|localhost|file)([\/\/]*[A-Za-z0-9]*[\.]*[A-Za-z0-9]*[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*)*$/;

        return regex.test(value);

    }, "Please enter a valid url");



    /**

     * validator the disk email

     */

    $.validator.addMethod("emailType", function (value, element, params) {

        var regex = /^.+@[^\.].*\.[a-z]{2,}$/;

        return regex.test(value);

    }, "Please enter a valid email");

    /**

     * validate total field length

     * params[0] the total character length of fileds specified by params[1]

     * params[1] the selector array specify which field need to be validate

     * e.g.

     * fieldLength:[254,['input[name="tagKey"]','input[name="tagValue"]']]

     * means field tagKey and tagValue total length must be less than or equal to 254 characters.

     */

    $.validator.addMethod("fieldLength", function (value, element, params) {

        var totalLength = 0;

        for (var key in params[1]) {

            totalLength = totalLength + $(params[1][key]).val().length;

        }

        return params[0] >= totalLength;

    }, $.validator.format("field{1} total length should be less than {0}"));


    /**

     * validate text input

     */

    $.validator.addMethod("text", function (value, element, params) {

        var reg = "/^(\w){" + params[0] + "," + params[1] + "}$/";

        var tel = new RegExp(reg);

        return this.optional(element) || (tel.test(value));

    }, "Can only input 0-20 letters, Numbers, underscores");

    /**

     * validate cidr

     */

    $.validator.addMethod("cidr", function (value, element, params) {

        if (this.optional(element)) {

            return true;

        }

        //determine whether the cidr only contain one '/'

        if (value.lastIndexOf("/") === -1 || value.lastIndexOf("/") != value.indexOf("/")) {

            return false;

        }

        var arr = value.split("/");

        var ipPart = arr[0];

        var segmentPart = parseInt(arr[1]);

        //not a number then return false

        if (isNaN(segmentPart)) {

            return false;

        }

        //check with ipv4 rule

        if (($.validator.methods.IPV4Type(ipPart))) {

            return segmentPart >= 0 && segmentPart <= 31;

        }

        //check with ipv6 rule

        if (($.validator.methods.IPV6Type(ipPart))) {

            return segmentPart >= 0 && segmentPart <= 127;

        }

        return false;

    }, "malformed cidr");

});


你可能感兴趣的:(JQuery validator 通用验证)