0

I have email field and label

<asp:TableRow runat="server">
                <asp:TableCell runat="server">
                    <asp:TextBox runat="server" ID="txtUserEmail" onfocusout="emailVerification()" CssClass="forTextbox ui-corner-all" PlaceHolder="Enter your email"></asp:TextBox>
                </asp:TableCell>
                <asp:TableCell runat="server">
                    <asp:ScriptManager runat="server" ID="smForEmail"></asp:ScriptManager>
                    <asp:UpdatePanel runat="server" ID="upLblEmail">
                        <ContentTemplate>
                            <asp:Label runat="server" ID="lblEmail"></asp:Label>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </asp:TableCell>
            </asp:TableRow>

And some other fields as well.

I want when user move to next field onfocusout="emailVerification()" method is called. In this method I want to check either email is exists in database or not. For this I write stored procedure.

USE [CDistributors]
GO
/****** Object:  StoredProcedure [dbo].[sp_InsertUser]    Script Date: 2/17/2016 2:50:05 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_InsertUser]
(@Email varchar(50) = null,
@ReturnValue int = null)
As
Begin
SET NOCOUNT ON;
IF EXISTS (SELECT UserId from Users where Email = @Email)
Begin

    return 0; -- Email already registered
End 
Else
Begin
    return 1; -- Email is not registered yet
End
End

And JavaScript function is

// code for email verification
$(function () {
    function emailVerification() {
         // What code to call stored procedure that return integer
    }
});

Please help.

1
  • 1
    this is very bad idea. You should make server side POST/GET validation method and ask this method from javascript. Commented Feb 17, 2016 at 10:09

1 Answer 1

1

You can make web service for database call methods it or make it as webmethod on the page

IF you are using .Net Framwork 4 or later you can specify ClientIDMode="Static" for controls to have on client the same Id as you specified

<asp:TextBox runat="server" ID="txtUserEmail"  ClientIDMode="Static" onfocusout="emailVerification()" CssClass="forTextbox ui-corner-all" PlaceHolder="Enter your email"></asp:TextBox>

Here WebMethod call example

// code for email verification
$(function () {
    function emailVerification() {
        // What code to call stored procedure that return integer
        var email = $('#txtUserEmail').val();
        jQuery.ajax({
            url: 'yourPage.aspx/VerifyEmail',
            type: "POST",
            data: "{'email' :' " + email + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
                alert("Maybe you need some stuff before! ");
            },
            success: function (data) { alert(data.d); },
            failure: function (msg) { alert("Something go wrong! "); }
        });
    }
});

code behind file

[System.Web.Services.WebMethod]
public static int VerifyEmail(string email)
{
    //Your verification code 
    return 1;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Aleksey, its solve my issue. But I make little changes instead of giving pageName.aspx I make Web services and it work fine. Else it gives some security error of password fields. Once again thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.