Friday, August 5, 2022

[FIXED] How to avoid adding empty username and password to my database?

Issue

When i try testing my add user method with an empty username and password. It still adds the users to my database without username and password. How can i check if my username || password is empty?

I tried this:

public boolean addUser(String userName, String password) throws Exception {
        //Checking if the username or password are empty
        if(userName == null || password == null) {
            throw new IllegalArgumentException("username or password can't be empty");
        }

        String sql = "insert into user(username,password) values(?,?) ";

But this doesn't work.


Solution

Your string could be the "empty" string or contain white space (ie spaces) which could cause it to fail.

Instead use something like:

if(userName == null || userName.isBlank()) 
{
    throw new IllegalArgumentException("username can't be empty");
}

// repeat test for password


Answered By - camickr
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.