2

I am trying to show alert if the input is not valid, but OnClientClick is not working, here is my code:

function validation1() {

    if (document.getElementById('firstname').value == "" || document.getElementById('firstname').value == "First Name..." ){
    alert("Please Enter First Name");
    } 
    if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
    alert("Please Enter Last Name");
    }
}


<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="validation1" />

My code is in master page, so could this be an issue?

Have tried many different ways to call the function using OnClientClick but no success:

2
  • 2
    Try OnClientClick="validation1()" Commented Jan 8, 2013 at 12:34
  • Have tried that, tried semi colon... Commented Jan 8, 2013 at 12:38

2 Answers 2

2

You're missing your parenthesis (brackets) from the function call, you might also want the result of the validation function determine if the server side click should fire:

function validation1() {

if (document.getElementById('firstname').value == "" ||document.getElementById('firstname').value == "First Name..." ){
   alert("Please Enter First Name");
   return false;
} 

if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
   alert("Please Enter Last Name");
   return false;
}

return true;
}

<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="return validation1();" />

The main difference here is that the client click function is returning a value to state if the validation passed or not as well as the actual method call declared correctly, easy thing to miss :).

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

1 Comment

@Wasi - OK, do you have any idea why it's not working? Using a javascript debugger like Firebug/Chrome do you see any script errors? If you put an laert at the top of the validation function, is it called? If so then the code maybe failing at the point of trying to get the element because the id is incorrect.
0

Try this

    function validation1() {

    if (document.getElementById('firstname').value == "" || document.getElementById('firstname').value == "First Name..." ){
    alert("Please Enter First Name");
    return false;
    } 
    if(document.getElementById('lastname').value == "" || document.getElementById('lastname').value == "Last Name..."){
    alert("Please Enter Last Name");
    return false;
    }
}

Here is aspx code

<asp:Button runat="server" ID="SearchButton" Text="Instant Search" OnClick="SearchButton_Click" OnClientClick="return validation1();" />

1 Comment

You must "return bla();" to stop/defer the postback.

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.