简体   繁体   中英

How to make a text field value unique?

I have a form with text field. I want to make the text field entry unique so that people should not be able to save the same name again.

Can we make the field unique in the table column?

or

How can we write a JS code to make this field unique?

This is what I am doing at the backend.

public static boolean isUniqueShortName(String sname)
throws DataObjectException
{
    return isUniqueShortName(sname, null);
}


public static boolean isUniqueName(String sname, BigDecimal id)
throws DataObjectException
{

    final String SELECT_SQL = "SELECT COUNT(*) FROM LISTS WHERE LIST_NAME = ? AND ID <> ?";
    final String SELECT_SQL2 = "SELECT COUNT(*) FROM LISTS WHERE LIST_NAME = ?";

    boolean exists = false;
    Connection conn = DataObjectConnectionPool.getInstance().getConnection();
    PreparedStatement pstmt = null;
    ResultSet result = null;

    try
    {
        // determine SQL
        String SQL = null;
        if (list_id != null)
        {
            SQL = SELECT_SQL;
        }
        else
        {
            SQL = SELECT_SQL2;
        }

        // prepare statement
        pstmt = conn.prepareStatement(SQL);

        // set parameters
        pstmt.setString(1, sname);
        if (id != null)
        {
            pstmt.setBigDecimal(2, id);
        }

        // execute query
        result = pstmt.executeQuery();


        // fetch results
        while (result != null && result.next())
        {
            // fetch
            int count = result.getInt(1);
            exists = (count == 0);
        }

        // close results
        if (result != null)
        {
            try { result.close(); } catch (Exception e) {}
        }

        // close statement
        if (pstmt != null)
        {
            try { pstmt.close(); } catch (Exception e) {}
        }
    }
    catch (Exception ex)
{ 
// throw error
        throw new DataObjectException("Error checking for name uniqueness for " + sname);
    }
    finally
    {
        // close results
        if (result != null)
        {
            try { result.close(); } catch (Exception e) {}
        }

        // close statement
        if (pstmt != null)
        {
            try { pstmt.close(); } catch (Exception e) {}
        }


        // close connection
        if (conn != null)
        {
            try { conn.close(); } catch (Exception e) {}
        }
    }

    return exists;
}

There is no way you can make the field unique at client side. you can do a validation on the server and say the name already exists on the client side.

like doing an Ajax request to see if the name exists.

having the table column is one way, but you also need to look where you are indexing upon, if its a string with a large length, i suggest you look for another index key.

You definitely want to make that field unique in the database, and then add javascript to validate but do not solely rely on JS validation. In MySQL, you can make it a primary key or add a unique index for that column. That will throw an error if you try to add the same name, so make sure you catch that error with whatever technology youre using on backend ( php or whatever ) and let your user know that name is taken. At that point you can add an ajax call to your server to validate the name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM