0

When I click the asp.net button as per the below code, it goes into my js file, and gets the function as I need, however if it fails validation it still goes through with the postback as if it were valid.

the asp.net button

<asp:Button ID="bttnSend" runat="server" OnClientClick="DoValidation()" Text="Send" CssClass="btn btn-primary margin30" />

the javascript

function DoValidation(parameter) {
console.log("validating");
var valid = true;
var emailTo = document.getElementById("txtEmailTo").value;
if (emailTo.length < 1) {
    alert("Please select at least one recipient to send an email to");
    valid = false;
}

console.log(valid);
if (valid == true) {
    __doPostBack('bttnSend', parameter);
}

};

I would be grateful if someone could please tell me what i need to change and what to so that the validation doesnt allow the postback if it fails.

thanks

1

1 Answer 1

4

You need to prevent the default action of button when condition fails.

Modify your function to return true/false

function DoValidation(parameter) {
    var valid = true;
    var emailTo = document.getElementById("txtEmailTo").value;
    if (emailTo.length < 1) {
        alert("Please select at least one recipient to send an email to");
        valid = false;
    }
    return valid;
};

The use the return value

<asp:Button ID="bttnSend" runat="server" OnClientClick="return  DoValidation()" Text="Send" CssClass="btn btn-primary margin30" />
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, i can accept the answer in 10 minutes according to this so will accept when possible

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.