I'm getting a JavaScript error (in IE only of course) and I can't figure out why. I assumed it was a trailing comma or something but I can't find one. I'm hoping I'm overlooking something and maybe one of you can see what I missed.
My control and custom validator:
<asp:TextBox runat="server" ID="txtName" MaxLength="100" CssClass="styled" Columns="50" />
<asp:CustomValidator runat="server" ID="cvName" ErrorMessage="Enter a valid contact name or email address" ControlToValidate="txtName" Display="None" ValidationGroup="PlatformContact" ClientValidationFunction="doesUserExist" />
<asp:ValidatorCalloutExtender ID="vceName" runat="server" TargetControlID="cvName" WarningIconImageUrl="~/img/icons/ic_asterisk.gif" CssClass="validatorStyled" PopupPosition="Right" CloseImageUrl="~/img/icons/ic_x_close_orange.png" />
<asp:RequiredFieldValidator runat="server" ID="valName" ErrorMessage="Enter a contact name or email address" ControlToValidate="txtName" Display="None" ValidationGroup="PlatformContact" />
<asp:ValidatorCalloutExtender ID="vceNameRequired" runat="server" TargetControlID="valName" WarningIconImageUrl="~/img/icons/ic_asterisk.gif" CssClass="validatorStyled" PopupPosition="Right" CloseImageUrl="~/img/icons/ic_x_close_orange.png" />
And here is the JavaScript/jQuery I am using:
<script language="javascript" type="text/javascript">
var userExists = true;
function doesUserExist(source, args) {
var txtName = $('#<%= txtName.ClientID %>').val();
$.ajaxSetup({ cache: false });
$.ajax({
type: "POST",
contentType: "application/json",
data: "{name:'" + txtName + "'}",
url: "ManageMyContacts.aspx/DoesUserExist",
dataType: "json",
success: function (result) {
userExists = result.d;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//Something bad happened,redirect to login page
window.location.href = '<%= ResolveUrl("~/Default.aspx") %>';
}
});
args.IsValid = userExists;
}
</script>
Any insight is greatly appreciated.
EDIT: JavaScript error
Message: 'controltovalidate' is null or not an object
Here is the WebMethod I user to check for the user name (in the code behind)
[WebMethod(EnableSession = true)]
public static bool DoesUserExist(string name)
{
ManageMyContactsService service = new ManageMyContactsService();
int index = name.IndexOf("[") + 1;
if (index > 0)
{
string email = name.Substring(index, name.Length - (index + 1));
return service.DoesUserExist(email);
}
else if (name.IndexOf("@") == -1)
return false;
else
return service.DoesUserExist(name);
}
I noticed the doesUserExist function gets called twice for some reason, can anyone tell why from this code?
It appears an UpdatePanel was causing the second ajax call and was causing IE to throw a JS error. Worked fine in the other browsers but not IE7/8!
Hope that helps someone with a similar issue.