Question

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?

Was it helpful?

Solution 4

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.

OTHER TIPS

The correct way for solving this problem would be to use CustomValidator control and its ClientValidationFunction property. With this you can integrate your client side validation with ASP.NET validation functionality.

Have a look at the example on MSDN documentation.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx

Hope it helps!

Regards,

Uroš

Your are using a Custom Validation function directly on button click. Look you are passing two parameters in the Function and OnClientClick you are not passing parameters. this method should be called through CustomValidator control of asp.net.

Or just remove parameters from method. Use method without parameters

well this is not the correct way to implement the custom validator look at this http://www.w3schools.com/aspnet/control_customvalidator.asp and http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_customvalidator (show you how to do it properly) but still if you want to do the validation like this you have to return false in case it is not valid

so your validation function should return true (in case it is valid) or false then

<asp:Button ID="btnSaveEnquiryRegister" runat="server" Text="Save Enquiry Register" CssClass="button" OnClick="btnSaveEnquiryRegister_Click" ValidationGroup="valEnquiry" OnClientClick="return RedirectForSaveValidation();"/> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top