首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >火狐上的CompareValidator问题

火狐上的CompareValidator问题
EN

Stack Overflow用户
提问于 2010-06-11 09:02:47
回答 4查看 1.1K关注 0票数 4

我需要在web表单应用程序中验证一个日期。为此,我使用CompareValidator与

代码语言:javascript
复制
Operator="DataTypeCheck" Type="Date"

问题是这个Validator在2位数的Firefox上不能正常工作。( javascript:m2是未定义的),它的年份为4位数,工作正常。

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

有人知道一个很好的解决办法吗?

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-06-14 07:41:35

我的解决方案是创建一个日期验证器,它继承自BaseValidator并覆盖ControlPropertiesValid(), EvaluateIsValid() and OnPreRender(EventArgs e)。如果你有其他想法,请开枪。

票数 0
EN

Stack Overflow用户

发布于 2012-09-19 15:59:53

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

微软已经修复了ASP.NET 4.0中的错误。

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

代码语言:javascript
复制
this.ClientScript.RegisterClientScriptInclude(
    typeof(System.Web.UI.WebControls.BaseValidator), "WebUIValidation.js",
    "url-to-the-4.0-WebUIValidation.js-script");

或者,您可以通过向页面上的一个ValidatorConvert块添加以下内容来覆盖buggy的<script type="text/javascript">函数:

代码语言: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();
    }
}
票数 3
EN

Stack Overflow用户

发布于 2010-06-11 09:28:17

也许这对您有帮助(最后一篇文章,只有dataType == "Date“需要),但我还没有测试它:http://forums.asp.net/t/1358621.aspx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3021526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档