ajax - How to validate username using Javascript? -


I want to know how I can use javascript if the user enters a user name when creating the account Has been present in the database and ask the user to type another user name?

  1. Blurring for & lt; Input / & gt; element.
  2. Use the AJAX send request from the server (with the field value as the parameter)
  3. Check on behalf of the server whether the user name is already in use or not
  4. Based on the server's response display (or not) This username is already in use Message

jQuery (I'm very lazy Pure JS) + PHP sample code:

  & lt; Form ... & gt; ... & lt; Input type = "text" name = "user name" id = "input-user name" /> & Lt; P class = "error" & gt; & Lt; / P & gt; ... $ ("#input-username"). Blur (function {$ .post ("/ check-username.php", {user name: $ (this) .val ()}, function (data) {if ("0" == data) {/ * Username Usage * / $ (this) .next ("p"). Text ("This username is already in use. & Lt; / p & gt;") Other} / * Username is correct / $ ( This) .next ("p") below ();}});}); & Lt ;? Php $ username = $ _POST ['username']; // Check whether the user name database exists in the $ usernameExists = ...; Copy $ usernameExists? '0': '1'; // if present, 1 if not  

Comments