2

I have page with 2 textboxes and button. When I press the button, some event appears on the server. But I need to check case if textboxes are empty. So I've made 2 events for button onclick="Button1_Click" onclientclick="Button1_javaEvent()". In event Button1_javaEvent I check this case. Is it possible to stop Button1_Click in javascript if textboxes are empty and how???

 function Button1_javaEvent() {
        var flag = false;
        var message_str = "";
        if (main_form.Text_login.value == "") {
                message_str += "Enter the login";
                message_str += "\n";
                flag = true;
            }
         if (main_form.Password_login.value == "") {
                message_str += "Enter the password";
                flag = true;
             }
             if (flag == true) {
                 alert(message_str);
             }
             else {                  
                   ????????????         
             }
         }

2 Answers 2

2

In your javascript function return true if you want a postback and return false if you don't want a postback.

Like this:

function Button1_javaEvent() {
    var flag = false;
        var message_str = "";
        if (main_form.Text_login.value == "") {
                message_str += "Enter the login";
                message_str += "\n";
                flag = true;
    }

    if (main_form.Password_login.value == "") {
                message_str += "Enter the password";
                flag = true;
    }

    if (flag == true) {
        alert(message_str);
    }

    return !flag;
}
Sign up to request clarification or add additional context in comments.

1 Comment

it's a bad idea to have multiple return statement in a function. store the value in variable to return and use that variable in return statement.
1

Use following code:

onclientclick="return Button1_javaEvent()"

Return true/false from Button1_javaEvent() function, if Button1_javaEvent() return true then postback will happen otherwise not.

Refer online sample to test this

1 Comment

Button1_javaEvent is client side event

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.