How to Throw an Error Message If Username Is Already Registered

Display error message in a correct way if username already exists in a registration webform (asp.net)

Here is another example:

protected void btnSignUp_Click(object sender, EventArgs e)
{
if (!CheckLogin(txtLogin.Text.ToString().Trim()))
{
//Register the user
}
else
{
lblLoginAvailable.Visible = true;
lblLoginAvailable.Text = "This login already exists in our system. Chooce another login please";
}
}

protected bool CheckLogin(string login)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("select id from users where lower(login) = lower(@login)", con);
cmd.Parameters.Add("@login", SqlDbType.VarChar).Value = login;
string id = "";
try
{
con.Open();
id = cmd.ExecuteScalar() == null ? "" : cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
//...
}
finally
{
con.Close();
}
if (String.IsNullOrEmpty(id)) return false;
return true;
}

I use this in my project and it works for 100%. Hope this code make your understanding clear and will help you.

How can I show an error message If the username already exists for creating an account?

You can use Error Handling.

def handle_singUp(request):
if request.method == "POST":
userName = request.POST['username']
fname = request.POST['fname']
lname = request.POST['lname']
email = request.POST['email']
pass1 = request.POST['pass1']
#pass2 = request.POST['pass2']

myUser = User.objects.create_user(userName,email,pass1)
myUser.first_name = fname
myUser.last_name = lname
try:
myUser.save()
messages.success(request,"Your account has been created!")
return redirect("/")
except:
messages.success(request,"The username is already exist!")
return redirect("/")

return redirect("/")

else:
return HttpResponse("404-Not found the page")

Node and Passport Handling Username Already Existing

This not an answer to your problem but I feel obliged to mention this:

users.forEach(function(user) {
if (user.username == newUser.username){
// flash username already exists
console.log("User Already Exists")
return res.redirect("/users/add");
}
});

Instead of doing the above, why don't you just have the DB system do that for you instead of iterating through a list of users in memory?
Like so:

users = await User.find({ username: { $eq: newUser.username }});

Checking if username already exists gives error kotlin

I translated the code below from java

That's not the correct way of "translating" that code from Java to Kotlin, since that answer provides a solution for getting data only once. So to be able to do that in Kolin programming language please use the following lines of code:

val rootRef = FirebaseFirestore.getInstance()
val allUsersRef = rootRef.collection("all_users")
val userNameQuery = allUsersRef.whereEqualTo("username", "userNameToCompare")
userNameQuery.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
for (document in task.result) {
if (document.exists()) {
Log.d("TAG", "username already exists")
val userName = document.getString("username")
//Do what you need to do with the userName
} else {
Log.d("TAG", "username does not exists")
}
}
} else {
Log.d("TAG", "Error getting documents: ", task.exception)
}
}


Related Topics



Leave a reply



Submit