How to validate in HTML forms

Asked

Viewed 360 times

0

How mandatory fields are made in HTML forms (those that commonly use an asterisk * to indicate mandatory)? There is a parameter directly in the tag input? If not, how is the validation done (for example, at the moment the user clicks to send, if the field was not filled a message is displayed and the form is not sent)?

  • Gonçalo, your question has been receiving votes to be closed as unclear, probably by the way you wrote. I took the liberty of editing it to try to help. Always remember that you can edit yourself to try to improve (by getting a negative or a vote to close), OK? : ) Good luck.

  • Browser-dependent HTML-only validation will not work. The ideal is that the validation is done via javascript with a more robust validation running in the background with a web language (php,java,c#...)

  • 1

    Thanks Luiz. Sorry for the inconvenience.

  • 1

    I understand why you may not have found this other question, but I’ll leave it here for future references: Input constraint "type date"

2 answers

3

Just put the required in the input tag

<form>
  <input type="text" name="name" required />
  <input type="submit" />
</form>

  • 1

    (attention: you have already won my +1 and I also voted and commented the same in the other existing answer). It would be very important for future reference of other users if you also described what is the support of this tag in browsers. Maybe even providing a Javascript alternative to the case where the tag is not supported. If you make a nice response like that, I’ll even offer a reward for it. :)

2


Use the required attribute:

<input type="text" name="obrigatorio" required>

Abcs!

  • (attention: you have already won my +1 and I also voted and commented the same in the other existing answer). It would be very important for future reference of other users if you also described what is the support of this tag in browsers. Maybe even providing a Javascript alternative to the case where the tag is not supported. If you make a nice response like that, I’ll even offer a reward for it. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.