Ahmad Abbasi
Ahmad Abbasi

Reputation: 1776

Don't submit form when I press enter key in a textbox

i have a text-box in my form. I write a JavaScript function to add some functionality when user enter some value in text box and then press Enter button. When I press Enter button, first , it perform my JavaScript functionality but then it shows the validations message(it submits the form). How to solve this problem

Here is my texbox

<asp:TextBox ID="txt_CopyFrom" runat="server" onkeypress="searchKeyPress(event);" ClientIDMode="Static"></asp:TextBox>

And here is my JavaScript function

function searchKeyPress(e) {
        if (e.keyCode === 13) {
                .
                .
                .

        }
        return false;
}

Upvotes: 2

Views: 5199

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Prevent the default action by using preventDefault()

function searchKeyPress(e) {
    e.preventDefault();
    if (e.keyCode === 13) {
            .
            .
            .

    }
    return false;
}

Upvotes: 9

Related Questions