User-1063667917 posted
Hi,
I am using a ModalPopupExtender, in this modal the user saves an email, but I need to validate that the email does not exists to avoid duplicates.
I can't allow the page to postback because the modal will hide, so I need a sample to do it async.
I've seen lots of samples of CustomValidator but not on a ModalPopupExtender or not checking database.
So far I have done this but it's not working:
AJAX:
function validateEmail(oSrc, args) {
var isValid;
if (args.Value == '' || args.Value == undefined || $('#<%= txtRFCc.ClientID%>').val() == '') {
// prevent from calling webservice when unnecessary
isValid = true;
args.IsValid = true;
}
else {
$.ajax({
type: "POST",
url: "invoice33.aspx/IsUserAvailable",
contentType: "application/json; charset=utf-8",
data: "{ 'username': '" + args.Value + "'," + $('#<%= IdC.ClientID%>').val() + "}",
dataType: "json",
async: false,
success: function (msg) {
isValid = msg.d;
}
//, error: function () { }
});
}
args.IsValid = isValid;
console.log('args.IsValid: ' + args.IsValid);
console.log('isValid: ' + isValid);
}
ASP:
<asp:CustomValidator ClientValidationFunction="validateEmail" runat="server" ID="rfcc" ForeColor="Red" Font-Bold="true" ErrorMessage="Already exists" ControlToValidate="txtRFCc" Display="Dynamic" ValidationGroup="nSave" />
Code behind:
<WebMethod()> _
Public Shared Function IsUserAvailable(ByVal username As String, ide As Int32) As Boolean
Dim sls As New Intra.Clients
If sls.ClientExists(ide, username) Then
Return True
Else
Return False
End If
End Function
Any hint?
Thanks in advance