[英]Datepicker Validation is not working on using jquery plugin
我有從諸如datepicker選擇/輸入日期的情況。 我為datepicker使用了jquery插件。 它工作得很好。
正如我之前所說,用戶還可以直接在文本框中輸入日期,而不是從日歷中選擇。 在這個階段,我們都知道用戶可能碰巧錯誤地輸入了日期。 因此我介入了jquery datepicker驗證插件。 在那里,我發現了一些文章要繼續。
有用的鏈接如下,
我嘗試的是:
正如第一個鏈接所說(Keith wood),我嘗試使用datepicker.validation.js。 但是當我輸入錯誤的日期時沒有任何反應。 以下是我試過的完整代碼,
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="UTF-8">
<title> Test for date picker</title>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/south-street/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker.validation.js"></script>
<script src="demo.js" type="text/javascript"></script>
</head>
<body>
<form id="validateForm" action="#">
<script >
$('#validateForm').validate({
errorPlacement: $.datepicker.errorPlacement,
rules: {
validDefaultDatepicker: {
required: true,
dpDate: true
},
validBeforeDatepicker: {
dpCompareDate: ['before', '#validAfterDatepicker']
},
validAfterDatepicker: {
dpCompareDate: { after: '#validBeforeDatepicker' }
},
validTodayDatepicker: {
dpCompareDate: 'ne today'
},
validSpecificDatepicker: {
dpCompareDate: 'notBefore 01/01/2012'
}
},
messages: {
validFormatDatepicker: 'Please enter a valid date (yyyy-mm-dd)',
validRangeDatepicker: 'Please enter a valid date range',
validMultiDatepicker: 'Please enter at most three valid dates',
validAfterDatepicker: 'Please enter a date after the previous value'
}
});
</script>
<p>
Select Date:
<input type="text" size="10" name="validDefaultDatepicker" id="validDefaultDatepicker"/></p>
<script>
$(function () {
$('#validDefaultDatepicker').datepicker();
});
</script>
</form>
根據第二個鏈接(chridam),我直接嘗試使用type = date概念。 它給了我希望,因為它完美地運作。 雖然解決方案很好,但它不適用於IE瀏覽器。 以下是完整的代碼,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
$("#jQueryValidateTest").validate();
$("[type=date]").datepicker({
onClose: function () {
$(this).valid();
}
});
});
</script>
<style>
.valid {
background: green;
color: #fff;
}
.error {
background: red;
color: #fff;
}
</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<form id="jQueryValidateTest">
<input type="date" required>
</form>
</td>
</tr>
</table>
</body>
</html>
希望我不要混淆你。 請幫助我克服這個障礙。 提前致謝。
如果我完全理解您的問題,您希望對日期進行自定義輸入驗證。 這是我最近在航班預訂網站上遇到的一個問題( 現場直播 )
有一點可以肯定的是, date
類型的輸入字段很好,但不是非常簡單的x瀏覽器,因為它們是自己的日期格式,具體取決於設備/操作系統/瀏覽器文化。 所以我的解決方案是text
輸入類型。 它還使用jquery UI datepicker和不顯眼的驗證插件。
為了克服這個障礙,除了缺少手動輸入驗證之外,jQuery UI datepicker中的IS的一大優勢是選項constrainInput
,其僅允許字符按其當前格式。 這已經是一個巨大的勝利!
在第二個注釋中,jQuery UI還附帶了文化文件,以針對自定義格式實例化datepickers。
在此基礎上,我相信需要一種自定義驗證方法。 然后,與您的設置相比,一個很大的區別可能是使用minDate
和maxDate
選項。 不確定這是否適用於您,但這就是我如何正確驗證它。
/**
* @description resolves an issue with chrome & safari date validation
* @param {String} format : regional dateformat
*/
setValidator: function (format) {
var self = this, // scope
settings = this.settings, // object literal
data = settings.data, // mapping for data-attributes
options = settings.options; // my custom behavior options
$.validator.addMethod('date', function (value, element) {
var inst, // jquery ui datepicker data inst object => very handy
parts, // split the user input
minDate = new Date(), // create a min range
maxDate = new Date(minDate), // create a max range
newDate; // the date to be validated in the end
// unrequired fields
if (this.optional(element)) {
return true;
}
try {
// get the data instance from the element, put there by jquery-ui
// warning: inline datepickers have their instance on the resp. div
inst = options.isInline ? self.findDatepicker($(element)).data(data.datepicker) : $(element).data(data.datepicker);
// determine the epoch timestamps of the input, based on the options minDate and maxDate, for calculation
minDate = typeof inst.settings.minDate === 'object' ? inst.settings.minDate.getTime() : minDate.setDate(minDate.getDate() + parseInt(inst.settings.minDate.replace(/[+d]/g, ''), 10));
maxDate = maxDate.setDate(maxDate.getDate() + parseInt(inst.settings.maxDate.replace(/[+d]/g, ''), 10));
// correct months 0-based
// correct 2digit years
// add hours to the maxDate
parts = value.split('/');
if (parts.length === 3) {
parts[0] = parseInt(parts[0], 10);
parts[1] = parseInt(parts[1], 10) - 1;
parts[2] = parseInt(parts[2], 10) < 100 ? parseInt(parts[2], 10) + 2000 : parseInt(parts[2], 10);
// notice how I switch the parts against the default format
newDate = new Date(parts[2], parts[1], parts[0], 23, 59, 59, 0).getTime();
if ($(element).data(data.maxdate)) {
newDate = newDate - (new Date(newDate).getHours() * 60 * 60 * 1000);
}
// unfortunately not very usefull as it doesn't return anything when false => value.split('/')
//$.datepicker.parseDate(format, value);
} else {
newDate = 0;
}
return newDate >= minDate && newDate <= maxDate;
}
catch (err) {
console.log(err);
}
});
}
基本上我遍歷每個jQuery datepicker:
'/scripts/lib/jquery-ui/ui/i18n/datepicker-{{culture}}.js'
i18n/datepicker-{{culture}} '/scripts/lib/jquery-ui/ui/i18n/datepicker-{{culture}}.js'
從i18n文化文件中獲取/設置文化 self.setValidator(regional.dateFormat);
格式設置驗證器self.setValidator(regional.dateFormat);
$('.datepicker').datepicker(options);
希望有一點意義或至少推動你朝着正確的方向前進。 如果有什么不清楚的地方,請不要問...因為我意識到這不是復制/粘貼解決方案^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.