I have the form to check username availability Where form should submit data only when username is available but form submit data in both case here is the code .
<script type="text/javascript">
$(document).ready(function () //When the dom is ready
{
$("#username").change(function () { //if theres a change in the username textbox
var username = $("#username").val(); //Get the value in the username textbox
if (username.length > 3) //if the lenght greater than 3 characters
{
$("#availability_status").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
//Add a loading image in the span id="availability_status"
$.ajax({ //Make the Ajax Request
type: "POST",
url: "http://localhost/Testtt/wp-content/plugins/Realestate Redirection/check_realitycode_availablity.php",
//file name
data: "username=" + username,
//data
success: function (server_response) {
$("#availability_status").ajaxComplete(function (event, request) {
if (server_response == '0') //if ajax_check_username.php return value "0"
{
$("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font> ');
//add this image to the span with id "availability_status"
} else if (server_response == '1') //if it returns "1"
{
$("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
}
});
}
});
} else {
$("#availability_status").html('<font color="#cc0000">Username too short</font>');
//if in case the username is less than or equal 3 characters only
}
return false;
});
});
</script>
This is JQuery to check Avaialablity.
Here is my Form
<form action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>" name="reality" method="post" id="reality_form" >
<input type="hidden" name="reality_hidden" value="Y">
Website Url:<input type="text" name="website_url" value="" />
Listing Code: <input type="text" name="rlt_code" id="username" /><span id="availability_status"></span>
<input type="submit" name="submit" value="submit" id="submit" />
</form>
I am trying to find the solutions from last few hours but there is no result . Please tell me what need to be add in code in condition where if(server_response == '1') to prevent form from submission in this Case
Well you could do something like mark the
<form>with a class initially:Then your “change” handler would clear that out on success:
Then, add a “submit” handler for the form:
You’ll have to check that the username is available on the server anyway, since you can’t be 100% sure that the AJAX check will have been done when the form is submitted.