check if username exists on form submit
- by Developer
This is my code.
.js
$(document).ready(function() {
    $("#username").focusout(function() {
        $.ajax({
            type:'post',
            url:site_url()+'/client/checkUserName',
            data:{'username':$("#username").val()},
            dataType:'json',
            success:function(result){
                if(result != 'false'){
                  $('#message').text('Username exists');  
                }
            }
        });  
    });
.html
<form id='form1'  method="post">
        <input type="text" name="username" id="username"/>
        <span id="message" style="color:red"></span>
        <input type="submit" value="submit" id="regis" class="btn"/>
</form>
Its working fine on focusout.
How to make the form to not submit if username exists? 
Suppose there is a hidden field in the form say
   <input type="text" hidden id="type" value="<?php echo $type;?>">
Then if value exists in the hidden field then the username neednot be checked if already exists or not.