Add required fields

Asked

Viewed 2,354 times

3

I wanted to know how do I put that fields Name, Email, Password,of my form are required to complete them. Follow my code below:

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="POST">
            <label><br />
                    Nome:
            </label><br />
                    <input type="text" name="nome" placeholder="Primeiro Nome" />
            <label><br />
                    Sobrenome:
            </label><br />
                    <input type="text" name="sobrenome" placeholder="Segundo Nome" />
            <label><br />
                    Email:
            </label><br />
                    <input type="text" name="email" placeholder="[email protected]" />
            <label><br />
                    Senha:
            </label><br />
                    <input type="password" name="senha" placeholder="********" />
            <label><br />
                    Confirmar Senha:
            </label><br />
            <input type="password" name="csenha" placeholder="********" /><br /><br />

            <input type="submit" value="Registrar" name="button" />

            <input type="reset" name="Redefinir" value="editar"/>
        </form>

        <?php

        //Função para conhecer o nome do botão
         function get_post_action($name)
            {
                $params = func_get_args();

                foreach ($params as $name) {
                    if (isset($_POST[$name])) {
                        return $name;
                    }
                }
            }

        //Verifica qual dos botões foi clicado, através do seu nome
        switch (get_post_action('button', 'submit', 'publish')) {
            case 'button':
                // Inclui classes
                include_once './cadastro.php';
                include_once './DAO/Conexao.php';

                //Instanciando Objeto
                $cadastro = new cadastro();
                $conexao  = new Conexao();

                //Buscar valores do formulário
                $nome       = $_POST['nome'];
                $sobrenome  = $_POST['sobrenome'];
                $email      = $_POST['email'];
                $senha      = $_POST['senha'];

                //Setando Objeto (Atribuindo valores ao objeto cadastro)
                $cadastro -> setNome($nome);
                $cadastro -> setSobrenome($sobrenome);
                $cadastro -> setEmail($email);
                $cadastro -> setSenha($senha);






                // Salvar dados
                $conexao ->saveCadastro($cadastro);

                echo "<script>alert('Cadastro realizado com sucesso! Agora é possível fazer login');</script>";



                break;

            case 'submit':
                //save article and redirect
                break;

            case 'publish':
                //publish article and redirect
                break;

            default:
            //no action sent
            }

        ?>
    </body>
</html>

1 answer

4


Thus:

jQuery:

$("input").prop('required',true);

If you’re using HTML5, add the attribute required:

<input type="text" name="nome_do_campo" required>

In $("input"), you can put a class type "required" and add in the fields you want to leave required.

  • Exactly what I needed! Thank you very much!

  • You’re welcome @Ruanrodrigues

Browser other questions tagged

You are not signed in. Login or sign up in order to post.