插件不显示修改后的消息 - jQuery Validate插件

时间:2017-05-09 19:36:39

标签: javascript jquery jquery-validate

jQuery验证器无法识别我为错误设置的消息。你可以帮帮我吗?

这是我的HTML文档:

<!DOCTYPE html>
<html lang="es">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Subir cursos</title>
    <link rel="stylesheet" href="css/jquery-ui.css">
    <link rel="stylesheet" href="css/jquery-ui.theme.css">
    <link rel="stylesheet" href="css/jquery-ui.structure.css">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8">
            <ol class="breadcrumb">
                <li><a href="index.php">Inicio</a></li>
                <li><a href="upload.php">Gestor</a></li>
                <li class="active">Subir Curso</li>
            </ol>
        </div>
        <div class="col-md-2">
            <a href="catalogo.php" class="btn btn-success"><span class="glyphicon glyphicon-plus" aria-hidden="true"> Subir Catalogo</a>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <h3 class="text-center">
                Subir curso
            </h3>
        </div>
    </div>
    <div class="row">
        <div class="col-md-3">
        </div>
        <div class="col-md-6">
            <form role="form" enctype="multipart/form-data" method="post" id="curso" action="R/rCurso.php">
                <p class="text-muted">Los campos marcados con (<span style="color:red">*</span>) son obligatorios</p>
                <div class="form-group">
                    <label>Curso <span style="color:red">*</span></label>
                    <input class="form-control" id="ccrs" name="crs" type="text" required data-validation="length" pattern="/^[ A-Za-z0-9_@./#&+-]*$/"/>
                </div>

                <div class="form-group">
                    <label>Indice </label>
                    <textarea class="form-control" name="idx" id="cidx" ></textarea>
                </div>

                <div class="form-group">
                    <label>Descripcion</label>
                    <textarea class="form-control" name="dsr" id="cdsr" ></textarea>
                </div>

                <div class="form-group">
                    <label>Fecha <span style="color:red">*</span></label>

                    <input name="fch" type="text" id="datepicker" required/>
                </div>

                <div class="form-group">
                    <label>Idioma <span style="color:red">*</span></label>
                    <select id="languaje" name="lng" required>

                    </select>
                </div>

                <div class="form-group">
                    <label>Imágen <span style="color:red">*</span></label>
                    <input id="imgPick" name="imgPick" value="" type="hidden">
                    <ul class="nav nav-tabs">
                        <li class="active"><a data-toggle="tab" href="#sel">Seleccionar Imagen</a></li>
                        <li><a data-toggle="tab" href="#upload">Subir imagen</a></li>
                    </ul>

                    <div class="tab-content">
                        <div id="sel"  class="tab-pane fade in active">
                            <h3>Selecciona una Imagen </h3>
                            <div class="row">
                                <div class="col-xs-12 col-md-12" id="pickimg">
                                </div>
                            </div>
                        </div>
                        <div id="upload" class="tab-pane fade">
                            <h3>Subir Imagen</h3>
                            <input id="exampleInputFile" name="input_field_name" type="file" />
                            <p class="help-block">
                                Solo archivos png, jpg, y jpeg de 600x600px
                            </p>
                        </div>

                    </div>
                </div>

                <div class="form-group">
                    <label>Enlace Youtube <span style="color:red">*</span></label>
                    <input class="form-control" name="url1" id="curl1" type="text" required/>
                </div>
                <div class="form-group">
                    <label>Enlace Vimeo</label>
                    <input class="form-control" name="url2" id="curl2" type="text" />
                </div>
                <div class="form-group">
                    <label>Enlace DailyMotion</label>
                    <input class="form-control" name="url3" id="curl3" type="text" />
                </div>
                <div class="form-group">
                    <label>Nivel <span style="color:red">*</span></label>
                    <select name="lvl"  id="clvl" required>
                        <option value="1">Superior</option>
                        <option value="2">Medio Superior</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Palabras clave <span style="color:red">*</span></label>
                    <input class="form-control" name="key" id="ckey" type="text" required/>
                </div>
                <div class="form-group">
                    <label>Autor <span style="color:red">*</span></label>
                    <select name="idAuth" id="idAuth"  required>

                    </select>
                </div>
                <div class="form-group"><script src="js/jquery.js"></script>
                    <label>Escuela <span style="color:red">*</span></label>
                    <select name="idEscu" id="idEscu" required>

                    </select>
                </div>
                <div class="form-group">
                    <label>Categoria <span style="color:red">*</span></label>
                    <select id="catg" name="idCatg"  required>
                    </select>
                </div>
                <div class="form-group">
                    <label>Subcategoria <span style="color:red">*</span></label>
                    <select id="subCat" name="idSubctg" required>

                    </select>
                </div>
                <button type="submit" id="save" class="btn btn-default">
                    Enviar
                </button>
            </form>
        </div>
        <div class="col-md-3">
        </div>
    </div>
</div>

<script src="js/webService.js"></script>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery-validate.js"></script>

这是我需要设置消息的脚本部分吗?我做了但是当我测试时,我收到了

的默认错误消息
  

此字段是必填字段。

这里是脚本部分,告诉我其他函数是否使代码工作正常或者我做错了什么

 $(document).ready(inicio);

    function inicio() {

        $(function () {
            $("#datepicker").datepicker();
        });

            $( "#curso" ).validate( {
                onkeyup: function (element, event) {
                    if (event.which === 9 && this.elementValue(element) === "") {
                        return;
                    } else {
                        this.element(element);
                    }
                },
                rules: {
                    ccrs: {
                        required: true,
                        minlength: 10
                    },
                    cidx: {
                        minlength: 10
                    },
                    cdsr: {
                        minlength: 10
                    },
                    datepicker: {
                        required: true,
                        date: true
                    },
                    languaje: {
                        required: true
                    },
                    curl1: {
                        required: true,
                        url: true
                    },
                    curl2: {
                        url: true
                    },
                    curl3: {
                        url: true
                    }
                },
                messages: {
                    ccrs: {
                        required:"Please enter your firstname",
                        minlength:"el minimo es 10"},
                    cidx: {
                        minlength:"your test must have more than 10 characters"},
                    cdsr: {
                        minlength:"your test must have more than 10 characters"},
                    datepicker: {
                        required: "Please set a date",
                        date: "It must be a Date"
                    },
                    languaje: {
                        required: "Please set any language"
                    },
                    curl1: {
                        required:"Please enter a valid email address",
                        url: "you must set an url"}
                }
            } );

        var autores = "#" + $('#idAuth').attr('id');
        var escuelas = "#" + $('#idEscu').attr('id');
        var categorias = "#" + $("#catg").attr('id');
        var subcategorias = "#" + $('#subCat').attr('id');
        var idiomas = "#" + $('#languaje').attr('id');
        listAutores(autores);
        listEscuelas(escuelas);
        listCategorias(categorias);
        listIdiomas(idiomas);
        $(categorias).one(function () {
            listSubcategorias(subcategorias, $(this).val());
        });
        $(categorias).on("change", function () {
            listSubcategorias(subcategorias, $(this).val());
        });
        listImgCursos("#pickimg");

    }//inicio

1 个答案:

答案 0 :(得分:1)

.validate()内,您将idname混为一谈......

rules: {
    ccrs: { // <- must match the NAME, not the ID!
        required: true,
        minlength: 10
    },
    ....
},
messages: {
    ccrs: { // <- must match the NAME, not the ID!
        required: "Please enter your firstname",
        minlength: "el minimo es 10"
    },
    ....

为简单起见,只需将idname设为相同。

<input id="ccrs" name="ccrs" type="text" ....

验证仍然有效的唯一原因是因为您在线设置了一些HTML5验证属性。例如,required仅起作用,因为您的内联required属性(这也是您看到默认消息的原因)。

顺便说一句,在.validate()方法中正确声明规则时,无需使用内联HTML5验证属性。请记住,jQuery Validate插件将动态禁用HTML5验证,因此某些HTML5属性将被jQuery Validate选中并使用,而其他属性将被忽略。

DEMO jsfiddle.net/psr9grq7/3/