jQuery验证在单独的文件.js中

时间:2016-06-04 15:08:55

标签: javascript jquery html

我有一个带有联系表单的HTML文件。我想要这个表单的jQuery验证,但在其他文件中,扩展名为.js。当在HTML文件中编写表单时,JQuery验证表单。但是,如果我将它转录为.js,它就无法运作。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br" lang="pt-br">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Escola Desafio - Contato</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <script language="javaScript" src="js/scripts.js"></script>
        <script language="javaScript" src="js/scripts1.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/jquery.validate.min.js"></script>
        <script type='text/javascript'>//<![CDATA[
            $(window).load(function(){
                $(document).ready(function () {
                    $('#frmContato').validate({ // initialize the plugin
                        rules: {
                            nome: {
                                required: true
                            },
                            field2: {
                                required: true
                            }
                        },
                        errorPlacement: function(){
                            return false;
                        },
                        submitHandler: function (form) { // for demo
                            alert('valid form submitted'); // for demo
                            return false; // for demo
                        }
                    });
                });
            });//]]> 
        </script>
    </head>
    <body>
<form id="frmContato" method="post" name="frmContato">
                    <input type="text" name="nome" id="nome" title="Insira o nome completo" placeholder="Nome Completo" onkeyup="AllowAlphabet()"/> 
                        <select name="assunto" id="assunto" title="Escolha um assunto" class="select">
                            <option value="" selected="selected">Selecione um assunto</option>
                            <option value="duvidas">Dúvidas/Informações</option>
                            <option value="falha">Falha/Erro/Indisponibilidade</option>
                            <option value="lentidao">Lentidão</option>
                            <option value="liberacao">Liberação/Bloqueio de Senha</option>
                            <option value="reclamacao">Reclamações/Críticas</option>
                            <option value="sugestao">Sugestões/Elogios</option>
                        </select> 
                    <input type="email" id="email" name="email" title="Insira o seu e-mail" placeholder="E-mail" />
                    <textarea placeholder="Escreva a sua mensagem aqui" rows="4" cols="50" class="textarea"></textarea>
                    <input id="btnEnviar" type="submit" value="Enviar" class="inline" onsubmit="return validateForm()"/>
                </form>
        </div>
    </body>
</html>

这是我的.js:

 window.onload = function(){
        $('#nome').keypress(function(e) {
            var keyCode = (e.keyCode ? e.keyCode : e.which); // Variar a chamada do keyCode de acordo com o ambiente.
                if (keyCode > 47 && keyCode < 58) {
                    e.preventDefault();
                }
        });
};

我试图这样做:

window.onload = function(){
    //alert("teste");
    //alert("teste");

    $('#nome').keypress(function(e) {
        var keyCode = (e.keyCode ? e.keyCode : e.which); // Variar a chamada do keyCode de acordo com o ambiente.
            if (keyCode > 47 && keyCode < 58) {
                e.preventDefault();
            }
    });
};

window.load = function(){
    $(document).ready(function () {
        $('#frmContato').validate({ // initialize the plugin
            rules: {
                nome: {
                    required: true
                },
                field2: {
                    required: true
                }
            },
            errorPlacement: function(){
                return false;
            },
            submitHandler: function (form) { // for demo
                alert('valid form submitted'); // for demo
                    return false; // for demo
                }
            });
        });
    });  
};

0 个答案:

没有答案