-1

This is my very first attempt on ajax! I'm unable to find a good post on the internet from where I could learn and apply to my project.

Let me show you the code straightaway!

    [WebMethod]
    protected bool CheckUsername(string username)
    {
        var check = (from c in dc.dml_np_Users
                     where c.Username.Equals(username)
                     select c).Count();
        if (check > 0)
        {
            return false; //return false if username exist in database
        }
        else
        {
            return true; //true if it doesn't exist, i.e available
        }
    }

markup with jquery is as follows:

<script type="text/javascript">
                            $('#txtUsername').blur(function () {
                                $(function () {
                                    $.ajax({
                                        type: "POST",
                                        url: "Login.aspx.cs/CheckUsername",
                                        data: JSON.stringify({ username: $('#txtUsername').val() }),
                                        contentType: "application/json; charset=utf-8",
                                        dataType: "json",
                                        success: function (data) {
                                            if (data) {
                                                toastr.success('username available');
                                            }
                                            else {
                                                toastr.error('Username already exist');
                                                $('#txtUsername').focus();
                                            }
                                        },
                                        error: function (data) {

                                        }
                                    });
                                });
                            });
                        </script>
                        <asp:TextBox ID="txtUsername" CssClass="form-control" runat="server" placeholder="Username"
                            AutoCompleteType="DisplayName" ValidationGroup="Signup"></asp:TextBox>

Now the jquery should include something like this: if(var available is true) success toastr should display else error toastr should display! I'm using toastr for the first time! Please correct my code!! Forgive me for asking too many things in just one question!!

The above code always shows the error msg!!

vohrahul
  • 1,123
  • 10
  • 17

2 Answers2

1

In my opinion change this url: "Login.aspx.cs/CheckUsername", to this url: "Login.aspx/CheckUsername",

Imir Hoxha
  • 1,674
  • 6
  • 31
  • 56
0

just do the verification?

                        <script type="text/javascript">
                     $(document).ready(function(){
                        $('#txtUsername').blur(function () {
                                $.ajax({
                                    type: "POST",
                                    url: "Login.aspx.cs/CheckUsername",
                                    data: JSON.stringify({ username: $('#txtUsername').val() }),
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function (data) {
                                        if (data) {
                                            toastr.success('username available');
                                        }
                                        else {
                                            toastr.error('Username already exist');
                                            $('#txtUsername').focus();
                                        }
                                    },
                                    error: function (data) {

                                    }
                                });
                        });
                 });
                    </script>
                    <asp:TextBox ID="txtUsername" CssClass="form-control" runat="server" placeholder="Username"
                        AutoCompleteType="DisplayName" ValidationGroup="Signup"></asp:TextBox>
Paulo Lima
  • 1,238
  • 8
  • 8
  • It always shows the error message!! no matter what I'm entering in the textbox! If I add a value which is not in database, it still shows the error messgage! I'm sure there's something wrong with my ajax call! :( – vohrahul Feb 13 '14 at 18:06
  • the parameter "data" is missing. url: "Login.aspx.cs/CheckUsername",data="username=name_current_user", ... – Paulo Lima Feb 13 '14 at 18:08
  • what is username = name_current_user? I mean what does each one mean? – vohrahul Feb 13 '14 at 18:11
  • The username is the parameter of your method, name_current_user is the value of textbox "txtUsername" ie the value that will be arriving on the server – Paulo Lima Feb 13 '14 at 18:15
  • but data would be a boolean as I'm calling a c# function which returns a bool using the webmethod? – vohrahul Feb 13 '14 at 18:18
  • the data that you are talking about is the return while I speak is the input (is the name of the User you look for a bank). – Paulo Lima Feb 13 '14 at 18:20
  • what error is occurring? this reaching the correct name on the server? – Paulo Lima Feb 13 '14 at 18:21
  • so should I pass "username=txtUsername.Value" in data? – vohrahul Feb 13 '14 at 18:23
  • There is no error! But the code is not behaving properly!!! ----> IT IS ALWAYS SHOWING THE ERROR MSG! <----- – vohrahul Feb 13 '14 at 18:25
  • I updated the code in my question!! Please have alook at it and tell me where am I going wrong now!!! – vohrahul Feb 13 '14 at 18:27
  • Are you sure your code is right? Why does it have if-else condition two times? – vohrahul Feb 13 '14 at 18:36
  • Doesn't show anything!! neither toastr.error nor toastr.success!! Tried diffrent values in textbox!! :(( – vohrahul Feb 13 '14 at 18:44
  • Although I still did not achieve the desired result! Infact nothing is populating! But you corrected my code to a great level, hence lead me in the right direction! Thus I;m marking it as an answer! Although it would be great if you try to sort my poblem! Thanks – vohrahul Feb 13 '14 at 19:12
  • Where are you calling from ajax? – Paulo Lima Feb 13 '14 at 19:37
  • will call only at page load, not when you enter a User Name – Paulo Lima Feb 13 '14 at 19:46
  • should i place the – vohrahul Feb 13 '14 at 19:49
  • make the call on the OnBlur event of the textbox – Paulo Lima Feb 13 '14 at 19:51
  • I updated my code!!! Please review! I tried everything you said! and Now I've come to this solution from some other forum's ref!! But still not getting to what I need!!! Damn! It's so frustrating tonight!!!! – vohrahul Feb 13 '14 at 19:57
  • where is the updated code? what did you change in the code? Anyway, i copy pasted your code! Still not working! No success msg, no error msg! simply nothing! – vohrahul Feb 13 '14 at 20:23