zoukankan      html  css  js  c++  java
  • 简述表单提交前如何进行数据验证

    通常在提交表单数据时,我们会对数据进行验证,例如某些字段是必填字段,不能为空,这时应该如何做呢?有如下三种方法:

    一、在button的submit事件进行判断

    <button type="submit">提交</button>
    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }
    
    ("#form").bind("submit",function(){
        var username= $.trim($("#username").attr("value"));
    
        if(isEmpty(username)){  
            alert("username不能为空。");
            
            return false;
       }else {
            return true;
        }  
    });

    二、在form的onsubmit事件判断

    注意: o nsubmit=“return false”为不执行提交;onsubmit=“return true”或 onsubmit=“return ”都执行提交。

    <form id="form" method="post" action="......"  onsubmit="return check()">
    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }
    
    function check(){  
        var username= $.trim($("#username").attr("value"));
    
        if(isEmpty(username)){  
            alert("username不能为空。");
            
            return false;
       }else {
            return true;
        }  
    }

    三、去掉submit类型button,直接用普通button

    <button type="button"  o nclick="check()">提交</button>  
    function isEmpty(obj){
        if(typeof obj == "undefined" || obj == null || obj == ""){
            return true;
        }else{
            return false;
        }
    }
    
    function check(){  
        var username= $.trim($("#username").attr("value"));
    
        if(isEmpty(username)){  
            alert("username不能为空。");
       }else {
            document.getElementById("form").submit();
        }  
    }
  • 相关阅读:
    windows下wchar_t* 转char*
    VS2010的调试参数/Zi /DEBUG
    fatal error LNK1112: module machine type 'x64' conflicts with target machine type 'X86'
    使用opencv传中文文件崩溃
    【20160924】GOCVHelper综述
    编译ITK
    几款开源图像处理软件评测研究
    新注册域名greenopen.site,向专业道路进军
    openmp在图像处理上面的运用
    实现multbandblend
  • 原文地址:https://www.cnblogs.com/Ge-Zsj/p/12524986.html
Copyright © 2011-2022 走看看