gpt4 book ai didi

asp.net - Firefox 上的 CompareValidator 问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:34 25 4
gpt4 key购买 nike

我需要验证 Web 表单应用程序中的日期。为此,我将 CompareValidator 与

Operator="DataTypeCheck" Type="Date"

问题是这个验证器在 2 位数年份的 Firefox 上不能正常工作。 (javascript 错误:m[2] 未定义)使用 4 位数的年份,它可以正常工作。

这里也描述了这个问题: https://connect.microsoft.com/VisualStudio/feedback/details/355573/comparevalidator-client-side-bug-two-digit-year-in-mozilla-based-browsers-throws-js-exception

有人知道解决这个问题的好方法吗?

谢谢

最佳答案

这是 ASP.NET 3.5 及更早版本客户端验证脚本中的一个错误。 (该脚本在旧版本的 Internet Explorer 中可以正常工作,但在符合标准的较新浏览器中不能正常工作。)

Microsoft 已修复 ASP.NET 4.0 中的错误。

如果不能升级到ASP.NET 4.0,可以从.NET 4.0自带的System.Web.dll版本中导出WebUIValidation.js资源,然后在页面的PreRender中注册脚本。事件:

this.ClientScript.RegisterClientScriptInclude(
typeof(System.Web.UI.WebControls.BaseValidator), "WebUIValidation.js",
"url-to-the-4.0-WebUIValidation.js-script");

或者,您可以覆盖 buggy ValidatorConvert通过将以下内容添加到 <script type="text/javascript"> 来实现功能在您的页面上屏蔽:

function ValidatorConvert(op, dataType, val) {
function GetFullYear(year) {
var twoDigitCutoffYear = val.cutoffyear % 100;
var cutoffYearCentury = val.cutoffyear - twoDigitCutoffYear;
return ((year > twoDigitCutoffYear) ? (cutoffYearCentury - 100 + year) : (cutoffYearCentury + year));
}
var num, cleanInput, m, exp;
if (dataType == "Integer") {
exp = /^\s*[-\+]?\d+\s*$/;
if (op.match(exp) == null)
return null;
num = parseInt(op, 10);
return (isNaN(num) ? null : num);
}
else if(dataType == "Double") {
exp = new RegExp("^\\s*([-\\+])?(\\d*)\\" + val.decimalchar + "?(\\d*)\\s*$");
m = op.match(exp);
if (m == null)
return null;
if (m[2].length == 0 && m[3].length == 0)
return null;
cleanInput = (m[1] != null ? m[1] : "") + (m[2].length>0 ? m[2] : "0") + (m[3].length>0 ? "." + m[3] : "");
num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
}
else if (dataType == "Currency") {
var hasDigits = (val.digits > 0);
var beginGroupSize, subsequentGroupSize;
var groupSizeNum = parseInt(val.groupsize, 10);
if (!isNaN(groupSizeNum) && groupSizeNum > 0) {
beginGroupSize = "{1," + groupSizeNum + "}";
subsequentGroupSize = "{" + groupSizeNum + "}";
}
else {
beginGroupSize = subsequentGroupSize = "+";
}
exp = new RegExp("^\\s*([-\\+])?((\\d" + beginGroupSize + "(\\" + val.groupchar + "\\d" + subsequentGroupSize + ")+)|\\d*)"
+ (hasDigits ? "\\" + val.decimalchar + "?(\\d{0," + val.digits + "})" : "")
+ "\\s*$");
m = op.match(exp);
if (m == null)
return null;
if (m[2].length == 0 && hasDigits && m[5].length == 0)
return null;
cleanInput = (m[1] != null ? m[1] : "") + m[2].replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((hasDigits && m[5].length > 0) ? "." + m[5] : "");
num = parseFloat(cleanInput);
return (isNaN(num) ? null : num);
}
else if (dataType == "Date") {
var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\.?\\s*$");
m = op.match(yearFirstExp);
var day, month, year;
if (m != null && (((typeof(m[2]) != "undefined") && (m[2].length == 4)) || val.dateorder == "ymd")) {
day = m[6];
month = m[5];
year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
}
else {
if (val.dateorder == "ymd"){
return null;
}
var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})(?:\\s|\\2)((\\d{4})|(\\d{2}))(?:\\s\u0433\\.|\\.)?\\s*$");
m = op.match(yearLastExp);
if (m == null) {
return null;
}
if (val.dateorder == "mdy") {
day = m[3];
month = m[1];
}
else {
day = m[1];
month = m[3];
}
year = ((typeof(m[5]) != "undefined") && (m[5].length == 4)) ? m[5] : GetFullYear(parseInt(m[6], 10));
}
month -= 1;
var date = new Date(year, month, day);
if (year < 100) {
date.setFullYear(year);
}
return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
}
else {
return op.toString();
}
}

关于asp.net - Firefox 上的 CompareValidator 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3021526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com