憶の年

js验证html表单不能为空
js代码<script type="text/javascript"> ...
扫描右侧二维码阅读全文
09
2016/06

js验证html表单不能为空

js代码

<script type="text/javascript">  
        function check(){  
            var nameValue=window.document.getElementById("name").value;
            var pasValue=window.document.getElementById("password").value;
            if (nameValue == "")
            {  
                window.alert("帐号不能为空!");  
                return false;  
            }else if (pasValue == "")  
            {  
                window.alert("密码不能为空!");  
                return false; 
            }
            return true;  
        }  
      </script>

html代码

   <form action="1.php" method="post" onsubmit="return check()">   
            <p>帐号: <input id="name" type="text" name="name" /></p>
            <p>密码: <input id="password" type="text" name="password" /></p>
            <input type="submit" value="登录"/>
    </form> 
Last modification:October 7th, 2018 at 09:23 pm

3 comments

  1. 麦葱

    <form action="1.php" method="post" onsubmit="return check()">

    &lt;p&gt;帐号: &lt;input id=&quot;name&quot; type=&quot;text&quot; name=&quot;name&quot; required/&gt;&lt;/p&gt; &lt;p&gt;密码: &lt;input id=&quot;password&quot; type=&quot;password&quot; name=&quot;password&quot; required/&gt;&lt;/p&gt; &lt;input type=&quot;submit&quot; value=&quot;登录&quot;/&gt; &lt;/form&gt;
    1. 憶の年
      @麦葱

      賣蔥大神

  2. 麦葱

    有个更简单的方法:

    <form action="1.php" method="post">

    <p>帐号: <input id="name" type="text" name="name" required/></p> <p>密码: <input id="password" type="password" name="password" required/></p> <input type="submit" value="登录"/>

    </form>

Leave a Comment