gpt4 book ai didi

javascript - 从按钮客户端单击上的验证控件触发 onclientclick 和客户端验证

转载 作者:行者123 更新时间:2023-12-04 00:12:34 24 4
gpt4 key购买 nike

我在页面上有几个 ASP 文本框控件,其中添加了自定义验证器。我有保存按钮,它可以验证这些文本框。我刚刚添加了与文本框相同的验证组。

为了合并附加要求,我为保存按钮添加了 onClientClick 函数。但有趣的是,由于验证组(验证器控件)而导致的验证没有触发,而是调用了 onClientClick 函数并调用了服务器端的 Click 事件。

Javascript 代码

function ValidateInputs(source, args) {
var regex = /^(-{0,1}\d{1,100})$/;
if (args.Value.length < 1) {
$("span[id$='spn_error']").html('Please Enter ' + $(source).attr("errormessage"));
$("span[id$='spn_error']").show();
args.IsValid = false;
return;
}
else if (!regex.test(args.Value)) {
$("span[id$='spn_error']").html($(source).attr("errormessage") + ' allows only numbers');
$("span[id$='spn_error']").show();
args.IsValid = false;
return;
}
else {
if ($("span[id$='spn_error']").html().indexOf($(source).attr("errormessage")) >= 0)
$("span[id$='spn_error']").hide();
args.IsValid = true;
return;
}
}

function isValidTracker() {
//Dummy Code Say Confirm button
return(confirm('Are you sure');)

}

HTML代码

<span class="errormesg" runat="server" id="spn_error" style="display: none;
font-size: 9px;"></span>
<asp:TextBox ID="txtLastMonthCount" runat="server" CssClass="inputbox" Width="75px" autocomplete="off" onkeypress="return isNumberKey(event);" ValidationGroup="g1"></asp:TextBox>

<asp:CustomValidator ID="CVLastMonthCount" runat="server" ErrorMessage="Last Months Closing Count" Text="<img src='../images/alert.gif' alt='Please Enter Last Months Closing Count' title='Please Enter Last Months Closing Count' />"
ValidationGroup="g1" ClientValidationFunction="ValidateInputs" OnServerValidate="ValidateInputs" ControlToValidate="txtLastMonthCount" ValidateEmptyText="true"></asp:CustomValidator>

<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="button" ValidationGroup="g1" OnClientClick="return isValidTracker();" OnClick="btnSave_Click" />

请帮助我找到触发 onclientclick 和保存按钮点击验证控制的解决方案。

最佳答案

将 OnClientClick 从 return isValidTracker() 更改为 if (!isValidTracker()) return false;

关于javascript - 从按钮客户端单击上的验证控件触发 onclientclick 和客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6897196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com