English 中文(简体)
手册表
原标题:Manual form validation in MVC 3 and JQuery

I want to be able to trigger form validation on the client side when the user selects the "GO" button. Currently when the "GO" button is selected it does not validate the complete form. For example, If I load the form and select the "Go" button without giving focus to the text box, nothing is validated and val.Valid() returns true. If I do enter invalid data in a text box, validation is ran for that individual item; and when I select the "GO" button, val.Valid() return false.

因此,我试图做的是,当用户选择一个子或其它活动时,我想启动所有形式验证。

I m 在MVC这样做 3

public class TestValidationModel
{
    [Required(ErrorMessage="UserName Is Required")]
    [RegularExpression(@"(S)+", ErrorMessage = "White space is not allowed")] 
    [StringLength(12, MinimumLength = 3)] 
    public string UserName { get; set; }

    [Required(ErrorMessage="Password Is Required")]
    [StringLength(20, MinimumLength = 3)] 
    public string Password { get; set; }

    [Required(ErrorMessage="Email Address Is Required")]
    [Display(Name = "Email Address")]
    public string EmailAddress{ get; set; }

}

<script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $("#butValidateForm").click(function(){

            var val = $( #myForm ).validate()
            alert(val.valid());
        })
    });
</script>

@using (Html.BeginForm("", "", FormMethod.Post, new {id = "myForm"}))
{
    <div>
       @Html.LabelFor(m => m.UserName)
       @Html.TextBoxFor(m => m.UserName)
       @Html.ValidationMessageFor(m => m.UserName)
    </div>

    <div>
       @Html.LabelFor(m => m.Password)
       @Html.TextBoxFor(m => m.Password)
       @Html.ValidationMessageFor(m => m.Password)
    </div>

    <div>
       @Html.LabelFor(m => m.EmailAddress)
       @Html.TextBoxFor(m => m.EmailAddress)
       @Html.ValidationMessageFor(m => m.EmailAddress)
    </div>

    <button id="butValidateForm">GO</button>
    <input type="submit" id="submitForm" value="Submit" />
}

A. 更新

我认为,我找到了问题的解决办法。 See shower Below

   <script type="text/javascript">
        $(function () {
            $("#butValidateForm").click(function () {

                var val = $( #myForm ).validate();
                val.showErrors();  //<-- Call showErrors
                alert(val.valid());
            })
        });
    </script>

This following link to the post on JQuery forms did not pertain to my problem, but I was able to find the method names I was looking for. http://plugins.jquery.com/content/jqueryvalidate-showerrors-issue-form-wizard

如果有人回答更好,请提供反馈。


A. 更新

先前的法典在欧安会中有些发挥作用,但根本不可行。 我做了以下工作,现在在欧罗纳开展工作,但还没有在国际发展学会工作。

    $(function () {
        $("#myForm").submit(function (e) {
            var val = $( #myForm ).validate(); //<--added the semi-colon
            val.showErrors();
            alert(val.valid());


            e.preventDefault();
        });
    });


//            $("#butValidateForm").click(function () {
//                var val = $( #myForm ).validate()
//                val.showErrors();
//                alert(val.valid());
//            })
    });

可查阅http://www.dotnetcurry.com/ShowArticle.aspx?ID=435>。 利用j Query To Hipill ASP.NET MVC Form Posts向我指出了正确的方向。 但是,这还不够完美。

问题回答

I don t know if this is the best/most efficient way, but I do it by validating each element individually in my own function.

jQuery(document).ready(function () {

    $("#butValidateForm").button().click(function () { return IsMyFormValid(); });
    $( #myForm ).validate();

}

function IsMyFormValid() {

    var isValid = false;

    isValid = $( #myForm ).validate().element($( #UserName  ));
    isValid = ($( #myForm ).validate().element($( #Password  ))) ? isValid : false;
    isValid = ($( #myForm ).validate().element($( #EmailAddress ))) ? isValid : false;

    return isValid;
}

允许它自动地验证,是很有道理的,但我似乎总是服从于某些令人厌恶的商业逻辑规则,而这种逻辑并没有被通用的验证方法所抓住。 通过这样做,我可以控制所有验证方法,但依靠在大部分时间进行验证。

<script type="text/javascript">
    $(function () {
        $("#butValidateForm").click(function (event) {

            var isValid = $( #myForm ).valid();
            if(isValid)
            {
               //do something before submit the form
               $( #myForm ).submit();
            }else{
               alert( validation failed ); 
            }
            event.preventDefault();
        })
    });
</script>

如果不确定你们是否如此,但我就与《国际法》中的同一件事情相去了:$(“#myForm”)。 使用<条码>

$("#myForm").live( submit , function (e) {
    var val = $( #myForm ).validate(); //<--added the semi-colon
    val.showErrors();
    alert(val.valid());


    e.preventDefault();
});

由于你失踪了半殖民地,所以没有在伊埃工作:

var val = $( #myForm ).validate()




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...