简体   繁体   English

如何使文本字段值唯一?

[英]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? 我们如何编写JS代码以使该字段唯一?

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. 比如做一个Ajax请求来查看名称是否存在。

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. 您肯定希望在数据库中使该字段唯一,然后添加javascript以进行验证,但不仅仅依赖于JS验证。 In MySQL, you can make it a primary key or add a unique index for that column. 在MySQL中,您可以将其作为主键或为该列添加唯一索引。 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. 如果您尝试添加相同的名称,这将引发错误,因此请确保您使用在后端(php或其他)上使用的任何技术捕获该错误,并让您的用户知道该名称已被采用。 At that point you can add an ajax call to your server to validate the name. 此时,您可以向服务器添加ajax调用以验证名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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