简体   繁体   中英

How to validate fields using jquery in asp.net mvc 3 razor

here my code:

@using (Html.BeginForm("Register", "User", FormMethod.Post, new { id = "RegisterForm" }))      
{
     @Html.TextBoxFor(m => m.EmailAddress, 
                      new { size = "40", 
                            @onchange = "UserNameVal();", 
                            @tabindex = "1" })
}

$("#RegisterForm").validate({

        rules: {
            EmailAddress: {
                required: true
            }
        },
        messages: {
            EmailAddress: {
                required: "<br/> Please enter the Email address"
            }
        }
    });
if ($("#RegisterForm").valid()) {
    $("#RegisterForm").submit();
}

Why don't you use annotation to validate the fields? it's much easier and cleaner, you can do as below:

 public class MyModel
 {
     [Required(ErrorMessage = "Please enter the Email address")]
     public string Email { get; set; }
 }

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