How to do dynamic form validation?

There is a registration form on the site:

<form action="" method="post" name="reg">
 <input type="text" size="20" maxlength="35" name="name" id="name" value="">
<form>

In a separate file, the function of checking the length (at least 2 and not more than 20 characters) and checking for a match in the database.

How to make sure that after the NAME field leaves the focus (I don’t know how to put it correctly), the data from the form is passed to the script, checked, and some html code is returned, or at least text (if everything is correct, one text, if no – different)

Smart people say it’s done via ajax. I kind of don’t mind, but I don’t know how to write a script.
Can you give an example of how to do this?


Answer 1, authority 100%

If there is a ready-made validation script, then, as I understand it, you only need an event. OnBlur – the opposite of OnFocus.

<input type="text" size="20" maxlength="35" name="name" id="name" value="" onblur="return validate();" />

And the validate() function validates the content accordingly. you can do validate(inputID, value) and write

onblur="return validate(this.id, this.value);"

Answer 2, authority 50%

the input tag also has a pattern attribute that allows you to set an input pattern for an element


Answer 3

Google “jquery form validation”. Couple of links: