Utilizing the required field validator to validate a text box and prompting for confirmation on the click of a submit button using the confirm() function in JavaScript has posed a challenge. Upon pressing OK in the confirmation box, the page refreshes and the required field validator fails to halt the process when the text box is left empty. After researching on Stack Overflow, I attempted to use a custom validator to prevent this issue, but unfortunately, I was unable to resolve it. Here is the code snippet:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ValidationGroup="one" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="one"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="validate();" ValidationGroup="one" onclick="Button1_Click"/>
<asp:CustomValidator ID="CustomValidator1" ValidateEmptyText="true" runat="server" ValidationGroup="one" ErrorMessage="CustomValidator"></asp:CustomValidator>
<script type='text/javascript'>
function validate() {
var cv = document.getElementById('MainContent_CustomValidator1');
if (cv) {
cv.isValid = confirm('Are you sure you want to update the record?');
}
} </script>