身高采集字段选型(ft' inches格式)及格式验证方法咨询
Great question! Let's walk through the best options for handling feet-and-inches height input like 5'6 or 6'0.
First, let's address the core question: should you use a string field? The short answer is yes if you want to preserve the exact user-friendly input format, but there are caveats if you need to work with the data programmatically (like sorting, converting units, or calculating BMI).
方案1:双字段存储(推荐)
If your application needs to both display the intuitive feet-inches format and perform calculations/sorting, I'd recommend storing two fields:
- A
stringfield (e.g.,height_display) to hold the raw user input like5'6for easy, intuitive display. - A
decimalorintegerfield (e.g.,height_in_inchesorheight_cm) to store the standardized numeric value for calculations. This avoids the hassle of parsing strings every time you need to do math.
For example, when a user enters 5'6, you'd parse it to 66 inches (5*12 +6) and store that in the numeric field, while keeping 5'6 in the string field. This gives you the best of both worlds: user-friendly presentation and easy data manipulation.
方案2:仅字符串字段
If you don't need to do any calculations or sorting on height, a string field works perfectly. Just make sure you enforce strict validation to keep the data consistent across all entries.
If you go with a string field, you'll need to validate that the input matches the X'Y format. Here's how to do it effectively:
正则表达式基础
A solid regex pattern for feet-and-inches (allowing optional double quotes for inches, like 5'6") would look like this:
^([1-7])'([0-9]|1[0-1])"?$
Let's break this down:
^and$ensure the entire string matches the pattern (no extra characters before/after).([1-7])matches feet from 1 to 7 (covers the vast majority of human heights).'matches the apostrophe separator.([0-9]|1[0-1])matches inches from 0 to 11 (since 12 inches = 1 foot)."?makes the optional double quote after inches allowed.
语言-specific验证示例
JavaScript
const heightRegex = /^([1-7])'([0-9]|1[0-1])"?$/; function validateHeight(input) { return heightRegex.test(input.trim()); } // Test cases console.log(validateHeight("5'6")); // true console.log(validateHeight("6'0")); // true console.log(validateHeight("5'12")); // false (invalid inches) console.log(validateHeight("8'2")); // false (invalid feet)
Python
import re height_pattern = re.compile(r"^([1-7])'([0-9]|1[0-1])'?$") def validate_height(input_str): return bool(height_pattern.match(input_str.strip())) // Test cases print(validate_height("5'6")) # True print(validate_height("6'0\"")) # True print(validate_height("5'12")) # False
Database-level validation (PostgreSQL example)
If you want to enforce validation at the database level, you can add a check constraint:
ALTER TABLE users ADD CONSTRAINT check_height_format CHECK (height ~ '^([1-7])''([0-9]|1[0-1])"?$');
Extra Edge Case Checks
Beyond regex, you might want to add logic to handle:
- Leading/trailing whitespace (always trim input first!).
- Inches values that are 12 or higher (either convert to feet automatically or reject the input).
- Unrealistic heights (like
1'0for an adult—adjust the feet range down to 0 if you're working with children's data).
内容的提问来源于stack exchange,提问作者Axioms

