简体   繁体   English

使用javascript验证html表单

[英]Validate html form with javascript

I want to validate user input with javascript before sending data to the server, but when I try the browser doesn't display the alert message. 我想在将数据发送到服务器之前使用javascript验证用户输入,但是当我尝试浏览器时不会显示警告消息。

I cannot find the problem. 我找不到问题。 Please help me. 请帮我。 Thank you! 谢谢!

My HTML: 我的HTML:

    <title>Login Page</title>

    <script type="text/javascript" src="../js/functions.js"></script>

</head>

<body>

    <div id="content">

         <form name="signin_form"  action="../processes/login_process.php" onsubmit="return checkLoginData()"  method="POST">

          <h1>Accesso Utente</h1>

          <p>
             <label for='tf_nick'>Nick</label>
             <input type='text' id='tf_nick' name='tf_nick' value="">

             <label for='tf_password'>Password</label>
             <input type='password' id='tf_password' name='tf_password' value=''>

             <input type='submit' value='OK'>
             <input type='reset' value='Pulisci'>
           </p>

        </form>

     </div>

</body>

My JavaScript: 我的JavaScript:

    function checkLoginData(){

        var formObj = document.signin_form;     
        var nick    = "" + formObj.tf_nick.value;
        var pass    = "" + formObj.tf_password.value;

        if ( nick == "" ) {
            alert("Inserisci il nick per accedere!");
            return false;

        } else if ( pass == "" ) {
            alert("Inserisci la password per accedere!");
            return false;

        } else {

            var regExNick = /(?=.*[0-9])(?=.*[a-zA-Z%])^[a-zA-Z0-9%]{3,6}$/;        
            var regExPass = /(?=.*[a-z])(?=.*[A-Z])^[a-zA-Z]{4,8}$/;

            if ( !regExNick.test(nick) ){
                alert("Nick non valido!");
                return false;

            } else if ( !regExPass.test(pass) ) {
                alert("Password non valida!");
                return false;

            } else {
                return true;
            }
        }
    }

This is the file system: 这是文件系统:

main_folder/pages/login.php
main_folder/js/functions.js

I created this jsfiddle which is just a copy/pasted from your code and there it works fine: http://jsfiddle.net/872d6ssn/1/ (code included below because stackoverflow demands that, but it's just a copy) 我创建了这个jsfiddle,它只是从您的代码中复制/粘贴的,并且在这里正常工作: http : //jsfiddle.net/872d6ssn/1/ (下面包含代码,因为stackoverflow要求这样做,但这只是一个副本)

Are you sure you include the js file in the correct way? 您确定以正确的方式包含js文件吗? I seems like you're not including the file at all. 我好像您根本不包含文件。

HTML 的HTML

      <h1>Accesso Utente</h1>

      <p>
         <label for='tf_nick'>Nick</label>
         <input type='text' id='tf_nick' name='tf_nick' value="">

         <label for='tf_password'>Password</label>
         <input type='password' id='tf_password' name='tf_password' value=''>

         <input type='submit' value='OK'>
         <input type='reset' value='Pulisci'>
       </p>

    </form>

JavaScript 的JavaScript

  function checkLoginData(){
    var formObj = document.signin_form;     
    var nick    = "" + formObj.tf_nick.value;
    var pass    = "" + formObj.tf_password.value;

    if ( nick == "" ) {
        alert("Inserisci il nick per accedere!");
        return false;

    } else if ( pass == "" ) {
        alert("Inserisci la password per accedere!");
        return false;

    } else {

        var regExNick = /(?=.*[0-9])(?=.*[a-zA-Z%])^[a-zA-Z0-9%]{3,6}$/;        
        var regExPass = /(?=.*[a-z])(?=.*[A-Z])^[a-zA-Z]{4,8}$/;

        if ( !regExNick.test(nick) ){
            alert("Nick non valido!");
            return false;

        } else if ( !regExPass.test(pass) ) {
            alert("Password non valida!");
            return false;

        } else {
            return true;
        }
    }
}

Thank you all for the answers. 谢谢大家的回答。 I tried the jsfiddle and it works, so I think that the problem is here: 我尝试了jsfiddle并且它起作用了,所以我认为问题出在这里:

<script type="text/javascript" src="../js/functions.js"></script>

Probably the source is not correct because the IE debugger says: 来源可能不正确,因为IE调试器显示:

'checkLoginData' is undefined

I have to set the correct path of my javascript file. 我必须设置我的javascript文件的正确路径。 The file system is like this: 文件系统是这样的:

main_folder/pages/login.php
main_folder/js/functions.js

Why this src="../js/functions.js" is not correct? 为什么此src="../js/functions.js"不正确?

Thank you for the help! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM