zoukankan      html  css  js  c++  java
  • 使用JS对form的内容验证失败后阻止提交 &&js校验表单后提交表单的三种方法总结

    1.form的两个事件

    submit,提交表单,如果直接调用该函数,则直接提交表单

    onSubmit,提交按钮点击时先触发,然后触发submit事件。如果不加控制的话,默认返回true,因此表单总能提交。

    2. JS的校验

    通过在JS中用document.myform.name.value,来得到用户的每一个输入 ,进行校验,当完全通过时,返回TRUE,反之返回false。

    3. 页面代码实现

    /*

    <form name="testform"  action="hello.html"  method="post" onSubmit="return check();">

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

      <input type="submit" value="提交">

    </form>

    */

    4. JS的实现

    function check(){
        if (document.testform.name.value=="admin")    {       
            alert("姓名不正确");       
            return false;   
            }
        else{
            return true;
            }
    }

    5.说明

     这里注意onSubmit的写法,千万不要写成:“check()”,这样当检验不能通过的时候不会提交表单。

    第一种:

    复制代码 代码如下:

    <script type="text/javascript">
             function check(form) {

              if(form.userId.value=='') {
                    alert("请输入用户帐号!");
                    form.userId.focus();
                    return false;
               }
           if(form.password.value==''){
                    alert("请输入登录密码!");
                    form.password.focus();
                    return false;
             }
             return true;
             }
    </script>

    <form action="login.do?act=login" method="post">
    用户帐号
      <input type=text name="userId" size="18" value="" >
    <br>
     登录密码     
    <input type="password" name="password" size="19" value=""/>     
     <input type=submit name="submit1" value="登陆" onclick="return check(this.form)"> 

    </form>  
     


    第二种

    复制代码 代码如下:

    <script type="text/javascript">
             function check(form) {

              if(form.userId.value=='') {
                    alert("请输入用户帐号!");
                    form.userId.focus();
                    return false;
               }
           if(form.password.value==''){
                    alert("请输入登录密码!");
                    form.password.focus();
                    return false;
             }
             return true;
             }
    </script>

    <form action="login.do?act=login" method="post" onsubmit="return check(this)">
    用户帐号
      <input type=text name="userId" size="18" value="" >
    <br>
     登录密码     
    <input type="password" name="password" size="19" value=""/>     
     <input type=submit name="submit1" value="登陆"> 

    </form> 


    第三种:

    复制代码 代码如下:


    <script type="text/javascript">
             function check(form) {

              if(form.userId.value=='') {
                    alert("请输入用户帐号!");
                    form.userId.focus();
                    return false;
               }
           if(form.password.value==''){
                    alert("请输入登录密码!");
                    form.password.focus();
                    return false;
             }

              document.myform.submit();
    }
    </script>

    <form action="login.do?act=login" name="myform" method="post">
    用户帐号
      <input type=text name="userId" size="18" value="" >
    <br>
     登录密码     
    <input type="password" name="password" size="19" value=""/>     
    <input type=button name="submit1" value="登陆" onclick="check(this.form)"> 

    </form>

  • 相关阅读:
    atitit.  web组件化原理与设计
    Atitit.git的存储结构and 追踪
    Atitit.git的存储结构and 追踪
    atitit.atiHtmlUi web组件化方案与规范v1
    atitit.atiHtmlUi web组件化方案与规范v1
    Atitit.设计模式-----触发器模式 trigger  详解
    Atitit.设计模式-----触发器模式 trigger  详解
    Atitit.web ui  组件化 vs  mvc
    Atitit.web ui  组件化 vs  mvc
    Atitit..css的体系结构
  • 原文地址:https://www.cnblogs.com/caicaizi/p/6066434.html
Copyright © 2011-2022 走看看