如何禁用表单提交,如果用户名不可用 [英] How to disable form submit if the username is not available

查看:155
本文介绍了如何禁用表单提交,如果用户名不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查用户名可用性。的问题是,即使用户名不可用,表单被张贴

I have checked the user name availability. The problem is, even if the username is not available, the form is posting.

编辑code:

<SCRIPT type="text/javascript">
$(document).ready(function(){

$("#emailch").change(function() { 

    var usr = $("#emailch").val();
    if(usr.length >= 3)
    {
        $("#status").html('<img src="images/loader.gif" align="absmiddle">&nbsp;Checking availability...');
        $.ajax({  
            type: "POST",  
            url: "includes/check.php",  
            data: "username="+ usr,  
            success: function(msg){   
                $("#status").ajaxComplete(function(event, request, settings){ 
                    if(msg == 'OK')
                    { 
                        $("#username").removeClass('object_error'); // if necessary
                        $("#username").addClass("object_ok");
                        $(this).html('&nbsp;<img src="images/tick.gif" align="absmiddle">');

                    }
                    else
                    {
                        $("#username").removeClass('object_ok'); // if necessary
                        $("#username").addClass("object_error");
                        $(this).html(msg);


                    }

                });

            }
        }); 
    }

    else
    {
        $("#status").html('<font color="red">The username should have at least <strong>3</strong> characters.</font>');
        $("#username").removeClass('object_ok'); // if necessary
        $("#username").addClass("object_error");
    }

});

});
</SCRIPT>

我想的是,的形式不应张贴如果用户名不可。我已使用返回false试图; 在IF的条件,但它失败。请提出任何替代方法。任何帮助将AP preciated。

What I want is that, the form should not be posted if the user name is not available. I have tried by using return false; in IF CONDITION but it fails. Please suggest any alternate method. Any help will be appreciated.

推荐答案

在提交返回false无论什么时候,如果字段是好做你的AJAX发送的形式,如果它的坏的,错误出或任何你需要

on submit return false no matter what, if the field is good do your ajax send of the form, if its bad, error out or whatever you need

<form action="http://www.google.com" id="formId" method="post">
<input type="text" id="emailch" name="emailch" />
<input type="submit" />
</form>


<script type="text/javascript">

$(document).ready(function(){

    $("#formId").submit( function() { 

        if( $("#emailch").val().length >= 3){
            alert( 'the form is going to post via ajax here' );
        }else{
            alert( 'user name invalid error out or whatever you need to do.' );
        }
        return false;
    });
});
</script>

这篇关于如何禁用表单提交,如果用户名不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆