Sunday 20 August 2017

jquery validation cumtom


$(document).ready(function () {

    /*
     *  Check initial space
     */
    $.validator.addMethod("noInitiallSpace", function (value, element) {
        return this.optional(element) || $.trim(value) != '';
    }, ("No space please and don't leave it empty"));



    /********check space starting and end ***********/
    $.validator.addMethod("noSpace",
            function (value, element) {
                return value.indexOf(" ") < 0 && value != "";
            }, ("No space please and don't leave it empty"));

    function getWordCount(wordString) {
        var words = wordString.split(" ");
        words = words.filter(function (words) {
            return words.length > 0
        }).length;
        return words;
    }

//add the custom validation method
    $.validator.addMethod("wordCount",
            function (value, element, params) {
                var count = getWordCount(value);
                if (count < 500) {
                    return true;
                }
            },
            ("Please enter no more than  {0} characters.")
            );


    /*********add validation for latter only *****/
    $.validator.addMethod("letter",
            function (value, element) {
                return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value);
            }, ('Only letters allowed!'));



    /***********check password 8 char 1 numeric 1 char*********/

    $.validator.addMethod("pwdcheck",
            function (value, element) {
                return /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,50}$/i.test(value);

            }, ('Password should be alphanumeric with minimum 8 characters.'));


    /***********check password 8 char 1 numeric 1 char*********/

    $.validator.addMethod("custom_number", function (value, element) {
        return this.optional(element) || value === "NA" ||
                value.match(/^[0-9]+$/);
    }, ("Please enter a valid number"));

    $.validator.addMethod("extension", function (value, element, param) {
        var ext = value.split('.').pop().toLowerCase();
        if ($.inArray(ext, param) == -1 && ext != '') {
            return false;
        }
        return true;
    }, ("Upload only pdf"));



})

No comments:

Post a Comment