2011-02-17 74 views
0

我有这个文本框:验证HTML文本框

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

我需要JS验证的帮助。如果用户有权限,他们可以输入任何日期。如果用户没有,他们只能输入当前的日期或将来的日期。

这是一个控制器方法,我可以得到,如果用户有权限与否。

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

Js/JQuery会是什么样子?我很难过。

谢谢!

回答

0

如果您使用jquery验证,请查看remote规则。

而且它最终可能会看起来像这样的事情:

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

如果你不希望使用现有的框架,你可以滚你自己的验证此一个字段。

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

在什么地方HasAdvanced方法发挥作用? – slandau 2011-02-17 20:27:05

0

一点题外话您HasAdvanced方法可以大大简化:

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