0

jQuery validator doesn't recognize the messages that I set for the error. Could you help me?

This is my HTML document:

<!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>

And this is the Script part where I have to set the messages right?, I did but then when I test, I get the default error message that is

This field is required.

here the script part, tell me if other functions make that the code works like that or if I did something wrong

 $(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
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • http://stackoverflow.com/questions/2457032/jquery-validation-change-default-error-message – Karthik Ganesan May 09 '17 at 19:43
  • BTW, there is no need to dump all that code here. Stripping the form down to one field and removing the extraneous scripts and tags would have fully demonstrated this problem. – Sparky May 09 '17 at 21:25

1 Answers1

1

Within .validate(), you've mixed up the id with the name...

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"
    },
    ....

For simplicity, just make the id and name the same.

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

The only reason validation was still working was because you set some HTML5 validation attributes inline. For example, required was only working because you had the required attribute inline (that's also why you saw the default message).

BTW, there is no need to have inline HTML5 validation attributes when you properly declare the rules within the .validate() method. Remember, the jQuery Validate plugin will dynamically disable HTML5 validation, so certain HTML5 attributes will be picked up and used by jQuery Validate, while others will be ignored.

DEMO: jsfiddle.net/psr9grq7/3/

Sparky
  • 98,165
  • 25
  • 199
  • 285