I'm trying to do Client-Side validation via a javascript function. I have an ASP.NET c# page, that successfully calls a WebMethod (from pages code behind) to validate data entered is accessible to the user. I have a textbox with AJAX AutoCompleteExtender and this works fine. I have CustomValidator that I'm trying to call a javascript function to validate the data. The webmethod (which pulls from SQL database) is executing and returning data as expected. The Javascript is executing the webmethod. The issue is the PageMethods is using an 'OnSuccess' function (a nested function from the javascript function being called) however from the OnScuess function I can't set the args.IsValid = true/false. I can set this property in the original function but in order to do that I need both the args.Value and result from the OnSuccess in order to evaluate if it is valid. Both are not accessible from each other. I've tried hiddenfields to set the value to evaluate, I've tried 'Global' variables in JavaScript. Nothing seems to give me the desired results. Both the hidden field as well as the 'Global' variable is not getting set before the evaluation to set the args.IsValid. Can someone please help. I can't believe how difficult such a simple task has become. The only way I'm getting the validation error message to show up is if I manually set the args.IsValid and issue an alert("doesn't mater what I put in here") at the bottom of the javascript primary function. I don't want an alert, I just want a message displayed next to my text box. Eventually this will go into a gridview (will have input boxes on each item row to allow multiple row edits at once.
I know a simple solution would be to have a dropdownlist but and serverside validation as well however this is not what I need.
Here is my javascript function
function pmValidateGLAccount(oSrc, args) {
var GLIsValid;
PageMethods.ValidateGLAccountAccess(args.Value, OnSuccess);
function OnSuccess(result) {
// I NEED TO DETERMINE IF IS VALID BUT SET THE args.IsValid after the process leaves this nested function
if (args.Value != result) {
GLIsValid = false;
}
else {
GLIsValid = true;
};
};// THIS IS THE END OF THE ONSUCCESS FUNCTION
//args.IsValid = false; //THIS IS WHERE WE NEED TO SET THE IsValid
} // THIS IS THE END OF THE pmValidateGLAccount FUNCTION
here is my CustomValidator
<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>
here is my webmethod
[System.Web.Services.WebMethod(), System.Web.Script.Services.ScriptMethod()]
public static string ValidateGLAccountAccess(string GLAccountNum)
{
string RetValue = string.Empty;
string CurUser = HttpContext.Current.User.Identity.Name.ToString();
string CurUserRemoveDomain = string.Empty;
int i = CurUser.IndexOf("\\");
if (i != -1)
{
CurUserRemoveDomain = CurUser.ToLower().Replace(Resources.AppSettings.ActiveDirectoryDomain.ToString().ToLower() + "\\", "");
CurUser = CurUserRemoveDomain;
}
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["CustDBConn"].ConnectionString;
using (SqlCommand cmd = new SqlCommand("MySproc", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SearchString", GLAccountNum);
cmd.Parameters.AddWithValue("@UserName", CurUser);
cmd.Connection = conn;
conn.Open();
RetValue = (string)cmd.ExecuteScalar();
conn.Close();
}
}
return RetValue;
}