我用两个ASP.NET -ui日期选择器扩展了jquery中的默认注册表单。
<div class="form-group">
@Html.LabelFor(m => m.LicenseDateOfIssuance, "Date of Issue", new { @class = "col-md-3 control-label required-field" })
<div class="col-md-9">
@Html.EditorFor(m => m.LicenseDateOfIssuance, new { htmlAttributes = new { @class = "form-control DateTimePicker", placeholder = "Date of Issuance", @readonly = "true" } })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.LicenseDateOfExpiry, "Date of Expiry", new { @class = "col-md-3 control-label required-field", placeholder = "eg 1 Jan 2015" })
<div class="col-md-9">
@Html.EditorFor(m => m.LicenseDateOfExpiry, new { htmlAttributes = new { @class = "form-control DateTimePicker", placeholder = "Date of Expiry", @readonly = "true" } })
</div>
</div>我想比较两个日期选择器持有的日期,并确保过期div的日期晚于发行div。
任何建议都将不胜感激。
发布于 2017-11-10 23:23:10
嗨,你可以使用万无一失的mvc扩展:http://foolproof.codeplex.com
或者你也可以使用MVC远程验证,这是我个人最喜欢的。下面是一个示例:
发布于 2017-11-10 23:42:35
我更喜欢使用MVC验证,但是如果你只想要客户端,那就试试这个脚本。它确实将日期格式化为日/月/年。
$(document).ready(function() {
var options = {
dateFormat: "dd/mm/yy"
};
$("#LicenseDateOfIssuance").datepicker(options);
$("#LicenseDateOfExpiry").datepicker(options);
$("#validate").click(function() {
var from = GetDate($("#LicenseDateOfIssuance").val());
var to = GetDate($("#LicenseDateOfExpiry").val());
if (from > to) {
alert("Invalid Date Range");
} else {
alert('ok');
};
});
});
function GetDate(date) {
var parts = date.split('/');
return new Date(parts[2], parts[1], parts[0]);
};<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>License of issuance: <input type="text" id="LicenseDateOfIssuance"></p>
<p>License of expiry: <input type="text" id="LicenseDateOfExpiry"></p>
<button id="validate">Validate range</button>
https://stackoverflow.com/questions/47224186
复制相似问题