开发者

Validate HTML Textbox

I have this textbox:

<%= Html.TextBox("EffectiveDate", Model.EffectiveDate.HasValue ? Model.EffectiveDate.Value.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "EffectiveDate", onchange = "parseAndSetDt(this); ", dataType = "Date" })%>

I need JS validation help. If the user has the permission, they can enter any date. If the user doesn't, they can only enter the present date, or dates in the future.

Here is a controller method I have that can get if the user has the permission or not开发者_如何学C.

[NoCache]
        public ActionResult HasAdvanced()
        {
            if (Chatham.Web.UI.Extranet.SessionManager.DisplayUser.IsInRole("hasICAdvanced"))
            {
                return Json(
                new
                {
                    value = "true"
                });
            }
            else{
                return Json(
                new
                {
                    value = "false"
                });
            }
        }

What would the Js/JQuery look like? I'm soooo stumped.

Thanks!


If you use jquery validation take a look at the remote rule.

And it might end up looking like something like this:

$("#myform").validate({
  rules: {
    EffectiveDate: {
      required: true,
      remote: "HasAdvanced"
    }
  }
});

If you do not want to use an existing framework you could roll your own validation for this one field.

$("#EffectiveDate").blur(function(){
  //or $.get
  $.post("HasAdvanced", null, function(data){
    //Handle the json data and update the field display a message, etc......
  }, "json");
});


As a sidenote your HasAdvanced method could be simplified greatly:

return Json(
    new { value = 
        Chatham.Web.UI.Extranet.SessionManager.DisplayUser.IsInRole("hasICAdvanced").ToString() 
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜