简体   繁体   中英

prevent The Client side validation go to server side

I have a form and I have a validation in JavaScript. How to prevent the submit button go the the server side if the form is not valid?

<button id="LoginButton" onclick="Login.initReuiredValidation();"
        onserverclick="LoginButton_Click" runat="server" type="submit" 
        class="submit btn btn-primary pull-right"> 
    <asp:Literal runat="server" meta:resourcekey="LoginButton" />                                    
    <i class="icon-angle-right"></i>                    
</button>

in jquery you can do as

 $(yourform).submit(function(event){
event.preventDefault();
//do somehting 
})

This isn't classic asp, it's asp.net webforms and I've edited the tags accordingly

Adding input validation in webforms is quite easy, you can assign a validation control to each form control, eg

Contact Name<br />
        <asp:TextBox ID="Contact_name"  runat="server" Width="246 pt"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ContactNameValidator" runat="server" 
            ErrorMessage="Please enter your name" ControlToValidate="Contact_name"></asp:RequiredFieldValidator><br />
        <br />
Contact Telephone<br />
        <asp:TextBox ID="Contact_phone" runat="server" Width="246pt"></asp:TextBox>
        <asp:RequiredFieldValidator ID="ContactPhoneValidator" runat="server" ControlToValidate="Contact_phone"
            ErrorMessage="Please enter your telephone number"></asp:RequiredFieldValidator>

If you view the output, you will see that the validation is actually done with (client side) JavaScript. Asp.net generates that for you, you don't have to write it yourself.

Further information here https://msdn.microsoft.com/library/a0z2h4sw%28v=vs.100%29.aspx

在后面的代码中添加...

LoginButton.Attributes.Add("onclick", "return false;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM