boot straphvalidatorフォームの検証、検証クリアリセット


//        
$(function () {
     formValidator();
 });
 // Modal      
$('#add').on('hidden.bs.modal', function () {
    $("#formValidate").data('bootstrapValidator').destroy();
    $('#formValidate').data('bootstrapValidator', null);
    formValidator();
});
//     
function formValidator() {
    $('#formValidate').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            hostname: {
                validators: {
                    notEmpty: {
                        message: '        '
                    },
                    regexp: {
                        // regexp: /([a-z0-9_-]{1,32}\.)+([a-z0-9_-]{1,32})((\.[a-z]{2,4})(.[a-z]{1,2})?)/,  xxx.xxx.xxx
                        regexp: /([a-z0-9_-]{1,32})((\.[a-z]{2,4})(.[a-z]{1,2})?)/,                     //   xxx.xxx
                        message: '         '
                    }
                }
            },
            ip: {
                validators: {
                    notEmpty: {
                        message: 'IP    '
                    },
                    regexp: {
                        regexp: /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/,
                        // regexp: /^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$/,
                        message: 'IP     '
                    }
                }
            }
        }
    })
}
//     
function saveTractor() {
    var bootstrapValidator = $("#formValidate").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (bootstrapValidator.isValid()) {
        var name = $("#hostname").val();
        var ip = $("#ip").val();
        var id = $("#tractorId").val();
        if (id == '' || id == undefined) {
            var tractorData = {
                "name": name,
                "ip": ip,
                "state": 0
            }
        } else {
            var tractorData = {
                "id": id,
                "name": name,
                "ip": ip,
                "state": 0
            }
        }
        $.ajax({
            type: "post",
            url: "/api/tractor/edit",
            beforeSend: function (request) {
                request.setRequestHeader("Authorization", localStorage.getItem("token"));
            },
            contentType: "application/json",
            data: JSON.stringify(tractorData),
            success: function (result) {
                if (result.code == 200) {
                    $("#add").modal("hide");
                    pageSearch();
                    $('#alertSuccess').removeClass('hide').addClass('in')
                    window.setTimeout(function () {
                        $('#alertSuccess').removeClass('in').addClass('hide')
                    }, 2000);
                } else {
                    $("#add").modal("hide");
                    $('#alertDanger').removeClass('hide').addClass('in')
                    window.setTimeout(function () {
                        $('#alertDanger').removeClass('in').addClass('hide')
                    }, 2000);
                }
            }
        })
    } else {
        return;
    }

}