check if username exists on form submit

Posted by Developer on Stack Overflow See other posts from Stack Overflow or by Developer
Published on 2012-12-12T16:35:10Z Indexed on 2012/12/12 17:03 UTC
Read the original article Hit count: 175

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript