Перейти к основному содержимому
← 📄 HTML & the platform·Модуль A3 · Урок 5
ЗаданиеBuild a username + password form: username input with name='username', required, minlength=3, maxlength=20, pattern='[A-Za-z0-9_]+'. Password input with name='password', required, minlength=8.

требуется, шаблон, минимальная длина, максимальная длина

100 XP8 min
Теория

HTML validation attributes — your browser's Pydantic

<input required />                                   <!-- can't submit empty -->
<input minlength="8" maxlength="64" />               <!-- length constraints -->
<input pattern="[A-Za-z0-9_]{3,20}" />               <!-- regex match -->
<input pattern="^\+\d{10,15}$" title="E.164 phone format like +14155552671" />

The browser blocks submit and shows a native tooltip. `title=""` customizes that tooltip.

Mirror your Pydantic constraints

class User(BaseModel):
    username: str = Field(min_length=3, max_length=20, pattern=r"^[A-Za-z0-9_]+$")

Becomes:

<input name="username" required minlength="3" maxlength="20"
       pattern="[A-Za-z0-9_]{3,20}" title="3-20 letters, digits, or underscores" />

Same constraint expressed twice, intentionally — the browser blocks 95% of bad input before it ever hits your endpoint, saving you bandwidth and giving instant feedback. Your Pydantic still validates on the server because malicious clients skip the browser.

The pattern attribute uses JS regex

NOT PCRE. NOT Python re. JavaScript regex syntax — most things work, but \d\w\s lookarounds etc. behave slightly differently. Test in the browser, don't trust your Python regex to copy-paste cleanly.

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.