HSNCRC.
TRTell Me About Your Project

Services

  1. Blog
  2. Web Development

HTML Contact Form: Labels, Validation And Accessibility

In a good contact form, every field has a visible label, the right input type and a clear error message. The validation the browser offers gives the user quick feedback; however, the submitted data needs to be checked again on the server. The examples below show this foundation.

Pair The Field With label

Every form field should have a label. The for value in the label is written the same as the field’s id value.

<label for="ad">Your Name</label>
<input id="ad" name="ad" type="text" autocomplete="name" required>

Thanks to this pairing, a screen reader says what the field is. Clicking the label also selects the field; on a small screen the touch target becomes larger.

placeholder does not take the place of a label. It disappears when the user starts typing, and what was asked for can be forgotten. I recommend always keeping the visible label.

Choose The Right input Type

The field type affects the mobile keyboard and the basic check the browser performs.

<label for="eposta">Email</label>
<input id="eposta" name="eposta" type="email"
       autocomplete="email" required>

<label for="telefon">Phone</label>
<input id="telefon" name="telefon" type="tel"
       autocomplete="tel" placeholder="05XX XXX XX XX">

<label for="mesaj">Your Message</label>
<textarea id="mesaj" name="mesaj" rows="5" required></textarea>

type="email" checks in the browser whether the entered value fits the email format. type="tel", on the other hand, does no format check; because phone numbers vary from country to country, the browser only opens the appropriate keyboard.

The autocomplete value tells the browser what kind of information is expected in the field. This way the user can fill in their saved information with a single tap.

required And Error Messages

If a field with required is left empty, the browser does not submit the form. This check is useful; still, also state in text which fields are required. Conveying it only with color or an asterisk is not enough for everyone.

An error message should state the problem and the fix together:

<label for="eposta">Email (required)</label>
<input id="eposta" name="eposta" type="email" required
       aria-describedby="eposta-hata" aria-invalid="true">
<p id="eposta-hata">
  The email address looks incomplete. Example: name@example.com
</p>

aria-describedby associates the error text with the field. Add the aria-invalid value only while the field is actually invalid. Instead of a phrase like “Invalid input,” write what is expected.

Also give clear feedback when the form is submitted: a text such as “Your message has arrived” keeps the visitor from sending the same form a second time. If you are going to state a response time, write the time you actually keep.

If you collect personal data, link to the privacy notice. If consent is required, the box must come unchecked; the user must check it themselves.

<input id="onay" name="onay" type="checkbox" required>
<label for="onay">
  I have read the <a href="/aydinlatma-metni/">privacy notice</a>.
</label>

Which text and which consent are required is a legal matter. Clarify your obligations under KVKK (Türkiye’s personal data protection law) with a specialist; the code here is only a markup example.

I recommend asking in the form only for the information you will actually use. If email is enough for the reply, do not ask for a date of birth.

Why Is Server-Side Validation Necessary?

The checks in the browser are there to help the user; they are not a security measure. A form request can also be sent from outside the browser, and the required or type attributes can easily be bypassed. OWASP’s input validation guide also states that client-side checks can be circumvented and that validation must be performed on the server.

On the server, check at least the following:

  • Are the required fields filled in?
  • Are the email and phone in the expected format?
  • Is the text length within a reasonable limit?
  • Is the incoming data made safe before it is displayed on a page or in an email?

Also plan a measure against unwanted submissions. Whichever method you choose, test that the form can be used with a keyboard and with a screen reader.

Frequently Asked Questions

Does Writing A placeholder Replace The Label?

No. placeholder is for showing an example; the label carries the name of the field.

Does A Form Work With HTML Alone?

HTML displays the form and sends the data. A server side or a form service that receives and processes the data is needed separately.

Should I Make The Phone Field Required?

If you are not going to reply by phone, I do not recommend making it required. Every unnecessary field makes the form longer.

You can continue with HTML link examples and the mobile usability checklist.

Sources

Would You Like Help With Corporate Website?

A website that presents your brand accurately and is easy to use on mobile.

Let’s Talk About What You Have In Mind.

A new website, better organized digital communication, or an idea you want to develop… We can work out where to start together.

Tell Me About Your ProjectWhatsApp