Diferencia entre revisiones de «Usuario:ManuelRomero/JavaScript/jquery/validate/solucionEJ4»

De WikiEducator
Saltar a: navegación, buscar
(Página creada con «<font size=5 color=red>VOLVER</font>»)
 
Línea 1: Línea 1:
 
[[Usuario:ManuelRomero/JavaScript/jquery/validate|<font size=5 color=red>VOLVER</font>]]
 
[[Usuario:ManuelRomero/JavaScript/jquery/validate|<font size=5 color=red>VOLVER</font>]]
 +
*El evento jvalidate viene definido con un parámetro especial
 +
*Este parámetro se conoce como objeto json
 +
*Un objeto json es una pareja parámetro valor separado por : y encerrado entre llaves
 +
'''''{parametro:valor}'''''
 +
*Como ya hemos visto en teoría tenemos que dar valores al elemento '''''rules'''''
 +
<source lang=html5>
 +
<!doctype html>
 +
<html>
 +
 +
<head>
 +
    <meta charset="UTF-8">
 +
    <title>Untitled Document</title>
 +
    <!--Estilos para cumplir con enunciado: -->
 +
    <style>
 +
        .important{color:red; font-weight:bold;}
 +
        label{display:inline-block; width:200px;}
 +
        input[type="submit"]{margin-left:200px;}
 +
    </style>
 +
    <!--Cargamos los scripts necesarios para poder hacer las validaciones:-->
 +
    <!--Los scripts se cargan siempre después de los css, nunca antes!-->
 +
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
 +
    <script type="text/javascript" src="jquery.validate.js"></script>
 +
</head>
 +
 +
<body>
 +
    <form id="formulario" novalidate action="accion.php" method="post">
 +
        <div>
 +
            <label for="nombre">Nombre:
 +
                <span class="important">*</span>
 +
            </label>
 +
            <input type="text" id="nombre" name="nombre" />
 +
        </div>
 +
        <div>
 +
            <label for="email">E-mail:
 +
                <span class="important">*</span>
 +
            </label>
 +
            <input type="email" id="email" name="email" />
 +
        </div>
 +
        <div>
 +
            <label for="url">Url:</label>
 +
            <input type="url" id="url" name="url" />
 +
        </div>
 +
        <div>
 +
            <label for="comentarios">Comentarios:</label>
 +
            <textarea id="comentarios" name="comentarios"></textarea>
 +
        </div>
 +
        <div>
 +
            <input type="submit" value="Enviar" />
 +
        </div>
 +
    </form>
 +
    <script>
 +
        $("#formulario").validate({
 +
                onkeyup: false,
 +
                onfocusout: false,
 +
                onclick: false,
 +
                rules: {
 +
                    nombre: {
 +
                        required: true,
 +
                        minlength: 2
 +
                    },
 +
                    email: "required",
 +
                    comentarios: "required"
 +
                }
 +
                });
 +
    </script>
 +
</body>
 +
 +
</html>
 +
 +
</source>

Revisión de 00:14 15 abr 2016

VOLVER

  • El evento jvalidate viene definido con un parámetro especial
  • Este parámetro se conoce como objeto json
  • Un objeto json es una pareja parámetro valor separado por : y encerrado entre llaves
{parametro:valor}
  • Como ya hemos visto en teoría tenemos que dar valores al elemento rules
<!doctype html>
<html>
 
<head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    <!--Estilos para cumplir con enunciado: -->
    <style>
        .important{color:red; font-weight:bold;}
        label{display:inline-block; width:200px;}
        input[type="submit"]{margin-left:200px;}
    </style>
    <!--Cargamos los scripts necesarios para poder hacer las validaciones:-->
    <!--Los scripts se cargan siempre después de los css, nunca antes!-->
    <script type="text/javascript" src="jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="jquery.validate.js"></script>
</head>
 
<body>
    <form id="formulario" novalidate action="accion.php" method="post">
        <div>
            <label for="nombre">Nombre:
                <span class="important">*</span>
            </label>
            <input type="text" id="nombre" name="nombre" />
        </div>
        <div>
            <label for="email">E-mail:
                <span class="important">*</span>
            </label>
            <input type="email" id="email" name="email" />
        </div>
        <div>
            <label for="url">Url:</label>
            <input type="url" id="url" name="url" />
        </div>
        <div>
            <label for="comentarios">Comentarios:</label>
            <textarea id="comentarios" name="comentarios"></textarea>
        </div>
        <div>
            <input type="submit" value="Enviar" />
        </div>
    </form>
    <script>
        $("#formulario").validate({
                onkeyup: false,
                onfocusout: false,
                onclick: false,
                rules: {
                    nombre: {
                        required: true,
                        minlength: 2
                    },
                    email: "required",
                    comentarios: "required"
                }
                });
    </script>
</body>
 
</html>