开发者

Validate HTML Textbox

开发者 https://www.devze.com 2023-02-10 23:13 出处:网络
I have this textbox: <%= Html.TextBox(\"EffectiveDate\", Model.EffectiveDate.HasValue ? Model.EffectiveDate.Value.ToString(\"dd-MMM-yyyy\") : \"\", new { @class = \"economicTextBox\", propertyName

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

精彩评论

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

关注公众号