日期:2014-05-17  浏览次数:20492 次

客户端验证
我想实现一个数量和金额之间的关系验证。之前是在服务器端验证的,后来老板说不符合要求,让改成用js验证的,可我对js一窍不通啊,求各位大虾指教,最好有代码啊!公式为:总金额(万元)<=产品数量*7000/10000元,如果不符合要求就给出一个提示!

------解决方案--------------------
<script type ="text/jscript" >
        function checkFormat() {
            var total = document.getElementById("total").value;
            var num = document.getElementById("num").value ;
            if (total <= num * 7000 / 10000) {alert("金额不符合要求!");return false;}
        }
    </script>

<div>
        请输入总金额:<input type ="text" id ="total"/><br /><br />
        请输入产品数量:<input type ="text" id ="num" /><br />
        <asp:Button runat ="server" ID ="btn_OK" Text ="ok" OnClientClick ="return checkFormat();" />
    </div>
------解决方案--------------------

function yanzheng() {
    var money = document.getElementById("金额的id");
    var number = document.getElementById("数量的id");
    if (money.value <= number.value * 7000 / 10000) { 
        alert("不符合要求!")
    }
}
然后用个事件调用这个方法就行了
------解决方案--------------------
onblur ="checkFormat()"加入到服务器控件TextBox的属性中去.
------解决方案--------------------
另外,如果是Validaion,服务器也要做验证,客户端验证有可能被绕过去。
------解决方案--------------------
那就给textbox绑定onblur事件
<asp:TextBox runat ="server" ID ="total" ></asp:TextBox>

protected void Page_Load(object sender, EventArgs e)
    {
        total.Attributes.Add("onblur","checkFormat()");
    }