Username Availability Check - Stop Submit if Taken
I have a check in place that queries the DB to check if a username is taken. My problem is that now I need validation on my form field that if taken don't allow the submit, but unsure on how to accomplish.
<script> $("#username").keyup(function (e) { //user types username on inputfiled var username = $(this).val(); //get the string typed by user $.post('checkUsername.php', {'username':username}, function(data) { //make ajax call to check_username.php $("#userResult").html(data); //dump the data received from PHP page }); });</script>
If you wan't the form stopping from submitting you can give it a try with .submit() event. When the form validation failed stop the default event en return false;
Always check your inputed data again at the server
Nathan Riley
ASKER
Not sure how I follow, how does your code know that the username is taken and to not allow the submit to happen?
Ray Paseur
The strategy here is to make an AJAX request to the server when the username field loses focus. The server can return a JSON string with a response value, and your JavaScript can take the appropriate action. This is what the checkUsername.php should do. It looks like you have many of the pieces in the right place, so what part do you still want help with?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
If you wan't the form stopping from submitting you can give it a try with .submit() event. When the form validation failed stop the default event en return false;
Open in new window
Always check your inputed data again at the server