使用HTML5验证功能检查现有用户名

I'm creating a registration form and I would like to check if the user already exist using PHP and HTML5 validation.

So somehow I would like to send the username that was typed in to a PHP function and write error message with setCustomValidity() in JavaScript (HTML5)

Can anyone offer me any advice to achieve this?

You can't do this automatically, if that's what you're asking. You'll need to provide validation on the server-side, too. HTML5 is client-side. And even if it did send usable validation parameters somehow, those could be falsified.

As for checking if a user already exists, this can't be done with HTML5. What you can do is client-side validation with Ajax.

To check username is already exist in DB, we need to write server side script. To handle this type of validation we can do using javascript ajax functionality. Using HTML5 you can't validate username or any value with DB.