public partial class FrmNewUser : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=SONIA-B408A4159\\SQLEXPRESS;Initial catalog=sonia;Integrated Security=true");
    string query;
    SqlCommand cmd;

    public string name="Code";

    protected void Page_Load(object sender, EventArgs e)
    {
              btnRegister.Attributes.Add("onclick", "javascript:return LenofPassword()");
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        try
        {
            query = "Insert into Newuser values(@UserName,@Password,@FullName)";
            cmd = new SqlCommand(query, conn);
            cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
            cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);
            conn.Open();
            cmd.ExecuteNonQuery ();
            conn.Close ();
        }
        catch(Exception ex)
        {
            lblErrors.Text =ex.Message.ToString();   
        }

       
    }
<title>New User Registration</title>
     <script type ="text/javascript" language ="javascript" >
     function LenofPassword()
     {
     var Password=document.getElementById("txtPassword");
      
       if(Password.value == "")
      {
        alert("Password should not be empty");
        Password.focus();
        return false;
       }
      
       if (Password.value.length <= 6 )
       {
        alert("Password length should be greater than 6");
        Password.focus();
        return false;
                  }
       
       
      var ConfirmPass=document.getElementById("txtConfirmPass");
          if(ConfirmPass.value == "")
      {
        alert("Confirm Password should not be empty");
        ConfirmPass.focus();
        return false;
            }
      
      if( Password.value != ConfirmPass.value)
      {
       alert(" Password  & Confirm Password do not match");
       return false;
       }
}

How Can i Check in JS Function that mine UserName already exists in database or not...

Well other than this is NOT a java forum, this is for c# and theres a .net web forum too.. The first thing is that you would have to use some form of ajax type thing as you would need to ask the db if the usernames there - but best advice, ask in a more apprioriate forum.

commented: He didn't mention Java. -2

I think he crosses 3 forums with this question since he didn't ask a specific question (c#, asp.net, sql).

BUT since you have the mechanism down to execute a query, I don't think its c#. Since you have the page designed, I don't think its asp.net. That leaves SQL (and how you deal with it in c#).

Assuming you're using MSSQL you can do something like (I don't have a compiler with me, so this hasn't been checked for compilation issues):

try
{
  query = "Insert into Newuser Select @UserName,@Password,@FullName Where Not Exists (Select * From NewUser x Where x.[UserNameColumn] = @UserName) Select Cast(@@ROWCOUNT as int) As Result";
  cmd = new SqlCommand(query, conn);
  cmd.Parameters.AddWithValue("@UserName", txtUsername.Text);
  cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
  cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);
  conn.Open();
  int rowCnt = Convert.ToInt32(cmd.ExecuteScalar());
  conn.Close ();
}
catch(Exception ex)
{
  lblErrors.Text =ex.Message.ToString();   
}

If the rowcount returns 0 then the username existed, if it returns 1 then you know it didn't. As a recommendation for best practices you should specify the columns in your Insert Into() statement since they're likely to change over time, which could break your query.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.