I've read several posts about this issue (here and in other sites) but no one is working for me. I'm trying to check if a given email is already registered in a database during the register process.
I've a page called Register.aspx in which I've declared the follow JavaScript function:
function check_email_availability(src, args) {
$.ajax({
type: "POST",
async: false,
timeout: 500,
contentType: "application/json",
url: "Register.aspx/CheckEmailAvailability",
data: "{ 'email' : '" + args.Value + "' }",
success: function (result) {
args.IsValid = result.d;
}
});
}
In the other hand, in the code behind in Register.aspx.vb, I've the follow WebMethod
:
<System.Web.Services.WebMethod()> _
Public Shared Functiion CheckEmailAvailability(ByVal email As String) As String
If Membership.GetUser(email) Is Nothing Then
Return "True"
Else
Return "False"
End Function
I'm trying to launch the JavaScript function through a CustomValidator
but I receive from Chrome's Console the follow message:
Uncaught TypeError: Cannot read property 'Value' of undefined
The error appears right in the line
data: "{ 'email' : '" + args.Value + "' }",
so -always using Chrome-, I've set a breakpoint on that line and, from Console, I've write
args.Value
and I've obtained the correct value, i.e., the email address that I had entered. So... I'm a little lost.
I appreciate any help.
Well, I finally made it work. I share here the used code. To get into context, in the user registration process, I want to check the email availability.
In the head of Register.aspx
:
<script type="text/javascript">
function check_email_availability(src, args) {
var isValid;
$.ajax({
type: "POST",
url: "Register.aspx/CheckEmailAvailability",
timeout: 500,
data: "{'email' : '" + args.Value + "'}",
contentType: "application/json; charset=utf-8",
async: false,
success: function (result) {
isValid = result.d;
}
});
args.IsValid = isValid;
}
</script>
In the body of Register.aspx
:
<asp:TextBox ID="tbEmail" runat="server" CssClass="TextBox" ClientIDMode="Static" TabIndex="1" />
<asp:CustomValidator ID="CValidatorEmail" runat="server" ControlToValidate="tbEmail" ValidateEmptyText="false" ClientValidationFunction="check_email_availability" ErrorMessage="Email already in use" SetFocusOnError="True" />
In the codebehind (Register.aspx.vb
):
<System.Web.Services.WebMethod()> _
Public Shared Function CheckEmailAvailability(ByVal email As String) As Boolean
Dim oMU As MembershipUser = Membership.GetUser(email)
If oMU IsNot Nothing Then Return False Else Return True
End Function
I hope this will be useful to someone. There is a question almost identical using C# that have help me to make it work.
Regards,