MVC视图中的Javascript验证 [英] Javascript validation in MVC view

查看:63
本文介绍了MVC视图中的Javascript验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC视图我正在使用



I have a MVC view in which I am using

@using (Html.BeginForm(ProductdetailSave,Product_Catalog, FormMethod.Post))





我有两个文本框MRP和售价。我想验证售价不应该大于MRP。所以我使用点击 - checkSP()。

它正在执行函数,但是它返回false,我的表单被提交



我尝试了什么:





I have two textboxes MRP and selling price . I want to validate selling price should not be greater than MRP . so I using on click- "checkSP()".
It is executing function but though it is returning false, my form gets submitted

What I have tried:

function checkSP()
{
    debugger;

    var MRP = $("#MRP").val();
    var SP = $("#Selling_price").val();

    if (parseInt(MRP) < parseInt(SP)) {
        alert("Selling Price Should be Less Than MRP");

        return false;
    }

}

推荐答案

#MRP)。val();
var SP =
("#MRP").val(); var SP =


#Selling_price)VAL();

if parseInt (MRP)< parseInt (SP)){
alert( 售价应该更低比MRP);

return false ;
}

}
("#Selling_price").val(); if (parseInt(MRP) < parseInt(SP)) { alert("Selling Price Should be Less Than MRP"); return false; } }


你可以使用onclick = return checkSP()



当你在onclick事件上调用一个方法(返回值)然后在该方法之前输入 return ,因为你的方法将返回值(true / false)。



谢谢,
You can use onclick="return checkSP()"

When you call a method (that return value) on an onclick event then put return before that method, because your method will return value (either true/false).

Thanks,


这篇关于MVC视图中的Javascript验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆