admin管理员组文章数量:1287637
I have few ASP Text Box controls on a Page, to which Custom Validators are added. I have Save Button, which validates these Text boxes. I have just added the Validation Group as same as that of the Text boxes.
In order to incorporate addtional requirement, I have added onClientClick function for Save button. But amused to find that Validation due to Validation Group(validator controls) is not firing instead the onClientClick function is called and server side Click event is called.
Javascript Code
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 Code
<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" autoplete="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" />
Please help me in finding the solution to fire onclientclick and validation control of Save button click.
I have few ASP Text Box controls on a Page, to which Custom Validators are added. I have Save Button, which validates these Text boxes. I have just added the Validation Group as same as that of the Text boxes.
In order to incorporate addtional requirement, I have added onClientClick function for Save button. But amused to find that Validation due to Validation Group(validator controls) is not firing instead the onClientClick function is called and server side Click event is called.
Javascript Code
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 Code
<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" autoplete="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" />
Please help me in finding the solution to fire onclientclick and validation control of Save button click.
Share Improve this question edited Aug 1, 2011 at 11:52 suryakiran asked Aug 1, 2011 at 10:33 suryakiransuryakiran 1,99626 silver badges41 bronze badges4 Answers
Reset to default 7Change OnClientClick from return isValidTracker()
to if (!isValidTracker()) return false;
Thanks for the Solutions provided. I too got a solution while googling, thought of sharing it, as it would be helpful in various other scenarios for others like me.
function isValidTracker() {
if (Page_IsValid) {
return(confirm('Are you sure');)//Dummy Code Say Confirm button
}
else
{
return false;
}
}
Where Page_IsValid is the Java script Variable set by the ASP.Net Validation Control. It is set to true when all the validation controls are successfully validated. Thanks :)
OneHalf's solution works, but I'd bake the final check into your validator - this ensures you're not asking the user "are you sure?" until you know their action is valid...
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;
}
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;
} else {
if ($("span[id$='spn_error']").html().indexOf($(source).attr("errormessage")) >= 0)
$("span[id$='spn_error']").hide();
args.IsValid = true;
}
if (args.IsValid) args.IsValid = isValidTracker();
}
Though I would agree that there's been an oversight on the ASP.NET team's part - your implementation seems more intuitive and should have been supported.
B
Hello I have also face the same issue. CausesValidation="true" property solved my problem.
本文标签:
版权声明:本文标题:javascript - firing both onclientclick and Client side validation from validation control on button Client click - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236225a2363046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论