简体   繁体   中英

Required Character Limit For Form Fields - DB, Ruby on Rails

I am wondering what is the best limit of characters for each form fields.

As I am creating a database, I wonder if how many characters it should required.

Some of the common fields are the ff:

  • first_name
  • last_name
  • email
  • username
  • password

Excited to your ideas guys. Please forgive me if I am asking a database related question here instead of programming. Let me know also if you know any more fields to add here. :)

These fields will likely be stored as a string, which depending on the underlying database will be 255 characters (usually).

If you wish to limit the length, you can do it by adding a validation to the model. This way Rails will validate it and display an error on the page if it is too long. You can also set a minimum length

validates_length_of :first_name, maximum: 50
validates_length_of :first_name, minimum:  2

You have different options to do so.

Method 1:

Add directly in DB:

t.string :first_name, :limit => 12, :null => false

If you already have the coulmn you can add a migration as.

change_column :users, :first_name, :string, :limit => 24

HEre is its definition:

Limit:

limit : Sets the maximum size of the string/text/binary/integer fields.

Method 2:

As the other answer explained, you can add it in Model so that you can handle validations easily.

validates :first_name, length: { minimum: 10 }
validates :first_name, length: { maximum: 24 }

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