locked
How to validate a username from database using bootstrap validator in asp.net RRS feed

  • Question

  • User1983779238 posted

    I want to validate a username from database using bootstrap validator in asp.net. This is some code i tried but not working... When i put remote, the username field is always invalid. Is this correct way to do so?

    username: {
                        validators: {                                                        
                            remote: {
                             url: '/Registration.aspx/CheckUsername',
                             type:'POST',
                             data: '{user: "' + $("#<%=username.ClientID%>")[0].value + '" }',
                                message: 'The username is not available.',
    
                            },
                            notEmpty: {
                                message: 'Please fill in your username.'
                            }
                        }
                    },
    [System.Web.Services.WebMethod]
        public static string CheckUsername(string user)
        {
            string retval = "";
            SqlConnection bbmsConn;
            string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            bbmsConn = new SqlConnection(connStr);
            bbmsConn.Open();
    
    
            SqlCommand cmd = new SqlCommand("select username from Accounts where username=@username", bbmsConn);
            cmd.Parameters.AddWithValue("@username", user);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                retval = "true";
            }
            else
            {
                retval = "false";
            }
    
            return retval;
        }



    Monday, March 20, 2017 3:39 AM

All replies