form表单提交前验证

一、

html

<form id="form1" method="post" action="/DealWithForm1/">   
<table>  
<tr>  
<td>first_name:</td>  
<td><input name="firstname" type="text" id="firstname" /></td>  
<td><label id="firstnameLabel"></label></td>  
</tr>  
<tr>  
<td>last_name:</td>  
<td><input name="lastname" type="text" id="lastname" /></td>  
<td><label id="lastnameLabel"></label></td>  
</tr>  
</table>  
<hr width="40%" align="left" />  
<button type="button" onclick="checktosubmit()">提交</button>  
<button type="button" onclick="jump();">取消</button>  
</form>

js

<script type="text/javascript">  
function jump()  
{  
    //清空表单所有数据  
    document.getElementById("firstname").value=""  
    document.getElementById("lastname").value=""  
    $("#firstnameLabel").text("")  
    $("#lastnameLabel").text("")  
}  

function checktosubmit(){  
    var txt_firstname = $.trim($("#firstname").attr("value"))  
    var txt_lastname = $.trim($("#lastname").attr("value"))  

    $("#firstnameLabel").text("")  
    $("#lastnameLabel").text("")  

    var isSuccess = 1;  
    if(txt_firstname.length == 0)  
    {  
        $("#firstnameLabel").text("firstname不能为空!")  
        $("#firstnameLabel").css({"color":"red"});  
        isSuccess = 0;  
    }  
    if(txt_lastname.length == 0)  
    {  
        $("#lastnameLabel").text("lastname不能为空!")  
        $("#lastnameLabel").css({"color":"red"});  
        isSuccess = 0;  
    }  
    if(isSuccess == 1)  
    {  
        form1.submit();  
    }  
}  
</script>

二、

html

<form id="form1" method="post" action="/DealWithForm1/" onsubmit="return check()">   

<input name="firstname" type="text" id="firstname" />
<button type="submit">提交</button>  

</form>

js

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

    var txt_firstname = $.trim($("#firstname").attr("value"))  
    $("#firstnameLabel").text("")  

    var isSuccess = 1;  
    if(txt_firstname.length == 0)  
    {  
        $("#firstnameLabel").text("firstname不能为空!")  
        $("#firstnameLabel").css({"color":"red"});  
        isSuccess = 0;  
    }  

    if(isSuccess == 0)  
    {  
        return false;  
    }  
    return true;  
    }  
</script>

三、

html

<form id="form1" method="post" action="/DealWithForm1/">   

<input name="firstname" type="text" id="firstname" />
<button type="submit">提交</button> 

</form>  

js

<script type="text/javascript">  

$(document).ready(function(){  

    $("#form1").bind("submit", function(){  
        var txt_firstname = $.trim($("#firstname").attr("value"))     
        $("#firstnameLabel").text("")  

        var isSuccess = 1;  
        if(txt_firstname.length == 0)  
        {  
            $("#firstnameLabel").text("firstname不能为空!")  
            $("#firstnameLabel").css({"color":"red"});  
            isSuccess = 0;  
        }  

        if(isSuccess == 0)  
        {  
            return false;  
        }  
        })  
    })  
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值