带参数的CustomValidator

时间:2015-06-15 20:58:19

标签: javascript c# validation parameters customvalidator

我使用CustomValidator和JavaScript验证TextBox,并将其传递给一些参数:

var CVH = { createFunction: function (validationFunction, extParamOne, extParamTwo) { var originalFunction = validationFunction; var extOne = extParamOne; var extTwo = extParamTwo; return function (src, args) { return originalFunction(src, args, extOne, extTwo); } } } var CustomValidatorHelper = CVH; function notEmpty(source, args, tbID, spID) { var textBoxId = document.getElementById(tbID); var spanID = document.getElementById(spID); if (textBoxId.Value == null || textBoxId.Value == "") { textBoxId.IsValid = false; textBoxId.className = "form-control redBorder" spanID.className = "redText"; alert(textBoxId.getAttribute('value')); } else { textBoxId.IsValid = true; textBoxId.className = "form-control" spanID.className = ""; alert(textBoxId.getAttribute('value')); } }

这是我的JavaScript

<asp:TextBox ID="tbFirstName" runat="server" class="form-control" autocomplete="name" MaxLength="20" ClientIDMode="Static" />

因此正确调用notEmpty并且它正在接收tbID和spID的值。

问题是,当我在TextBox中输入数据并执行代码时,我仍然没有得到价值。与textBoxId.Value一样,即使框中有数据也是NULL。

TextBox ID is correct

Not valid?

Texbox设置为静态:

<input name="ctl00$MainContent$tbFirstName" type="text" maxlength="20" id="tbFirstName" class="form-control" autocomplete="name" />

它正确渲染: library(data.table) library(microbenchmark) DT <- data.table(x = rep(c("a","aa","bb"),1000), y = rep(c("b","a","bbb"),1000)) DT1 <- copy(DT) DT2 <- copy(DT) DT3 <- copy(DT) DT4 <- copy(DT) microbenchmark( DT1[, test := grepl(x, y), by = x] , DT2$test <- apply(DT, 1, function(x) grepl(x[1], x[2])) , DT3$test <- mapply(grepl, pattern=DT3$x, x=DT3$y) , {vgrepl <- Vectorize(grepl) DT4[, test := as.integer(vgrepl(x, y))]} )

有关为什么它无法读取TextBox中的数据的任何建议?

1 个答案:

答案 0 :(得分:0)

所以错误在于尝试直接检查文本框的值而不是使用args:

function notEmpty(source, args, tbID, spID)
{
    var textBoxId = document.getElementById(tbID);
    var spanID = document.getElementById(spID);

    if (args.Value == "")
    {
        args.IsValid = false;
        textBoxId.IsValid = false;
        textBoxId.className = "form-control redBorder"
        spanID.className = "redText";
    }
    else
    {
        args.IsValid = true;
        textBoxId.IsValid = true;
        textBoxId.className = "form-control"
        spanID.className = "";
    }
}

所以代码仍然是相同的,除了更改args.value的textbox.value。