Jquery data verification plug in of self made simple practice example code

  • 2020-03-26 21:38:44
  • OfStack


(function($) {
    var defaults = {
        bugColor: '#FFCCCC', //Text box color when data is incorrect
        color: 'white', //Text box color when data is correct
        type: "alert", //Alert pop-up text assignment to span HTML when data is wrong
        msg: "Msg", //Prompt content when data is incorrect
        ResOjId: 'no'//The tag #id that is assigned when in test mode
    };
    function UiProcess(options, rexString, object) {
        var options = $.extend(defaults, options);
        var values = object.val();
        if (rexString.test(values)) {
            object.css("backgroundColor", options.color);
            return true;
        } else {
            object.css("backgroundColor", options.bugColor);
            if (options.type == "alert") {
                alert(options.msg);
            }
            if (options.type == "text") {
                $(options.ResOjId).html(options.msg);
            }
            return false;
        }
    }
    //Verify that the IP conforms to the format
    $.fn.RegIp = function(options) {
        var rexString = /^d{1,3}.{1}d{1,3}.{1}d{1,3}/;
        return UiProcess(options, rexString, this)
    }
    //Verify that the landline is formatted
    $.fn.RegTelPhone = function(options) {
        var rexString = /^[0-9]+[-]?[0-9]+[-]?[0-9]$/;
        return UiProcess(options, rexString, this)
    }
    //Verify that the phone conforms to the format
    $.fn.RegMombilePhone = function(options) {
        var rexString = /(^189d{8}$)|(^13d{9}$)|(^15d{9}$)/;
        return UiProcess(options, rexString, this)
    }
    //Verify that the Chinese language conforms to the format
    $.fn.RegCHZN = function(options) {
        var rexString = /[u4e00-u9fa5]/;
        return UiProcess(options, rexString, this)
    }
    //Verify that decimal conforms to the format
    $.fn.RegDecimal = function(options) {
        var rexString = /^[0-9]+[.]?[0-9]+$/;
        return UiProcess(options, rexString, this)
    }
    //Verify that decimal preserves a decimal number to conform to the format
    $.fn.RegDecimalSign = function(options) {
        var rexString = /^[+-]?[0-9]+[.]?[0-9]+$/;
        return UiProcess(options, rexString, this)
    }
    //Verify that the integer retention of a decimal is formatted
    $.fn.RegNumber = function(options) {
        var rexString = /^[0-9]+$/;
        return UiProcess(options, rexString, this)
    }
    //Verify that each integer retains a decimal number in the proper format
    $.fn.RegNumberSign = function(options) {
        var rexString = /^[+-]?[0-9]+$/;
        return UiProcess(options, rexString, this)
    }
    //Validates non-null characters
    $.fn.IsEmpty = function(options) {
        var rexString = /(^.+$)|([u4e00-u9fa5])/;
        return UiProcess(options, rexString, this)
    }
})(jQuery);

Call:


<script type="text/javascript">
        function submitOk() {
            var interfaceNameInput = $("#<%=interfaceName.ClientID %>");
            var userNameInput = $("#<%=userName.ClientID %>");
            var passWordInput = $("#<%=passWord.ClientID %>");
            var interfaceUrlInput = $("#<%=interfaceUrl.ClientID %>"); ;
            if (!interfaceNameInput.IsEmpty({ "msg": " Interface name format is incorrect! " })) { return false }
            if (!userNameInput.IsEmpty({ "msg": " Incorrect format! " })) { return false }
            if (!passWordInput.IsEmpty({ "msg": " Incorrect format! " })) { return false }
            if (!interfaceUrlInput.IsEmpty({ "msg": " Incorrect format! " })) { return false }
        }
    </script>


Related articles: