Validation input via HTML does not work

Asked

Viewed 166 times

1

I am trying to validate input using required and customizing the error message with oninvalid however when I fill in the field I tried to submit without filling in, for each character I type it shows the error message, ie the onchange does not change the status of the field.

Would anyone know what that could be?

 <input type="text" name="nome" id="nome" placeholder="Nome" required oninvalid="setCustomValidity('Insira o seu nome')" onchange="try{setCustomValidity('')}catch(e){}">

1 answer

1

Good guy, use Regex validation, is much easier than putting javascript, faster and lighter, goes straight into html.

ex.:

<input type="email" required pattern='\w*@\w*\.\w*\.?\w*'>

to pattern sets a pattern, and the code within it is Regex for validation

basic commands of Regex

\w = any letter, number and _ (underline/underline)

\ = defines that the successor character (ex.: '\.') must be himself and no other

? = defines that the previous character may or may not be placed

There are other commands needed but these are the most used and make it much easier, I hope to have helped :D

Browser other questions tagged

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