I am encountering an issue with client-side validation using a JavaScript function. On my ASP.NET C# page, I have a WebMethod that successfully validates data entered by the user. The page includes a textbox with an AJAX AutoCompleteExtender, which is working correctly. I am trying to use a CustomValidator that calls a JavaScript function to validate the data. The WebMethod, which retrieves data from a SQL database, is executing properly. However, I am facing a problem with setting the args.IsValid property within the OnSuccess function. The issue is that the args.Value and the result from the OnSuccess function are not accessible to each other, making it challenging to evaluate and set the validity of the data. I have tried using hidden fields and global variables in JavaScript, but they are not being set before the evaluation to set the args.IsValid. The only way I have managed to get the validation error message to display is by manually setting the args.IsValid and issuing an alert message at the end of the JavaScript function. I am looking for a solution that will display a message next to the textbox without the need for an alert. Ultimately, this validation will be implemented in a gridview with input boxes for multiple row edits. Utilizing a dropdown list with server-side validation is not suitable for my requirements.
Here is the JavaScript function I am using:
function pmValidateGLAccount(oSrc, args) {
var GLIsValid;
PageMethods.ValidateGLAccountAccess(args.Value, OnSuccess);
function OnSuccess(result) {
if (args.Value != result) {
GLIsValid = false;
} else {
GLIsValid = true;
}
};
}
Here is the CustomValidator code:
<asp:CustomValidator ID="CustomValidator1"
runat="server"
ControlToValidate="TextBox1"
ValidationGroup="vg_test"
ClientValidationFunction="pmValidateGLAccount"
ErrorMessage="Error: Please Enter an Account Number from the list"
Display="Dynamic"></asp:CustomValidator>
And here is the WebMethod code:
[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethod()]
public static string ValidateGLAccountAccess(string GLAccountNum)
{
// Code for the WebMethod
}