Search code examples
asp.netcustom-validators

Custom Validation not working in ASP.NET


I need to have a custom validation for a "Save" operation in my page. The requirement is that, I need to display the alert and when I click the OK button in the alert, my page should not be posted back. Here goes my code.

function RedirectForSaveValidation(source,arguments) {

    var StatusFlag = '';
    StatusFlag = document.getElementById('<%= HiddenStatusFlag.ClientID%>');

    if (StatusFlag == "F") {
        alert("Selected student entry has been qualified for lead. Entry cannot be modified...!");
        arguments.IsValid = false;
    }
    if (StatusFlag == "Q") {
        alert("Selected student has been scheduled for interview/counselling. Entry cannot be modified...!");
        arguments.IsValid = false;
    }
    if (StatusFlag == "S") {
        alert("Selected student entry has been scheduled with interview/counselling. Entry cannot be modified...!");
        arguments.IsValid = false;
    }
    if (StatusFlag == "I") {
        alert("Selected student entry has been converted to Intake. Entry cannot be modified...!");
        arguments.IsValid = false;
    }
    window.location.assign("EnquiryRegister.aspx");
}

I call this function in my button click.

<asp:Button ID="btnSaveEnquiryRegister" runat="server" Text="Save Enquiry Register" CssClass="button" OnClick="btnSaveEnquiryRegister_Click" ValidationGroup="valEnquiry" OnClientClick="RedirectForSaveValidation();"/>

The issue is, I am not getting any alert as I have specified and my page is posted back. What am I missing?


Solution

  • Silly me! The issue was with getting the value of my hidden field.

    var StatusFlag = '';
    StatusFlag = document.getElementById('<%= HiddenStatusFlag.ClientID%>').value;
    

    I had missed to include value property so the variable had empty value which caused the validator to fail.