1

recently ,I learn the Jquery to validate data in web. I used c# and B/S . now ,I have create a login form with server control box. like

now I have finish the validate, but the submit button still can work if validate is failed. now I want to set submit disable when validate is failed.

here is my a part of validate code

 $(document).ready(function () {

            $("#username").blur(function () {
                var name = $("#username").val();
                if (name == "") {
                    $("#nameShow").html("*Please input name");
                    $("#nameShow").addClass("warning");
                }
                else {
                    $("#nameShow").html("");
                    $("#nameShow").removeClass("warning");
                }

            });

            $("#username").click(function () {
                //当按钮点击的时候执行下面的代码
                //找到页面上id为userName的文本框,并获取文本框内的值,用jquery提供的val方法
                var userName = $("#username").val();
                $("#nameShow").removeClass("warning");
                //判断userName的值是不是为空
                if (userName == "") {
                    //如果为空,提示用户
                    $("#nameShow").html("*please input your login name.*");
                }
                else {
                    $("#nameShow").html("");

                }

            });

the c# Code:

  <asp:Button ID="login_btn" CssClass="btn" runat="server" Text="登录" OnClick="login_btn_Click" />

if validate false, then button cannot to be trigger. I used this way ,but it only work in IE, when switch to google browser ,it still can trigger.

  $("#login_btn").submit(function () {
                var name = $("#username").val();
                var pass = $("#password").val();
                var code = $("#verifycode").val();
                if (name == "" || pass == "" || code == "") {
                    return false;
                }
                else {
                    return true;
                }

            });

Can somebody help me with this?

1
  • I was just answering this question. If you haven't got the answer you could undelete and I will. If you have don't worry! Commented Nov 24, 2013 at 8:19

3 Answers 3

1

JS:

document.getElementById("login_btn").disabled=true;

JQuery:

$("#login_btn").attr("disabled", true); 

See more here: Submit disabled Property

If your button is an object, you can disable it like this:

submitButtonObject.disabled = true;

On valid form you can just change the attribute value from true to false.

Sign up to request clarification or add additional context in comments.

8 Comments

when validate failed then I set the button is disable. how to associate with validate functions?
Just add those lines to if statements. @chengdu.jack
I have some events like blur or click , how to use if statements with this function events?
@chengdu.jack try if (name == "") { $("#nameShow").html("*Please input name"); $("#nameShow").addClass("warning"); $("#login_btn").attr("disabled", true); } else { $("#nameShow").html(""); $("#nameShow").removeClass("warning"); $("#login_btn").attr("disabled", false); }
I need use blur or click event. when cursor focus on textbox or leap it ,then have show different message or color.
|
0

Try this

For Disable a Button

$('#YourButtonID').attr("disabled", true); 

Enable

$('#YourButtonID').attr("disabled", false); 

Comments

0

first of all you can use something like this as well:

$("#login_btn").submit(function (e) {
            e.preventDefault();
            var name = $("#username").val();
            var pass = $("#password").val();
            var code = $("#verifycode").val();
            if (name == "" || pass == "" || code == "") {
                return false;
            }
            else {
                $('your form').submit();
                return true;
            }

        });

1 Comment

I use server control . <form id="form1" runat="server">, is that can work, I try it and failed. just cannot find the submit() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.