自定义验证器中的控件名称 .NET 客户端验证

发布于 2024-08-20 17:37:14 字数 402 浏览 6 评论 0原文

我有一个带有三列文本框的网格视图。它可以根据需要拥有任意多的行,但通常只有大约 5 行。每一行都需要进行验证。

我想创建一个客户端验证器,将 2 列相加,并将其与第三列进行比较,以检查用户是否正确输入了数据。

以防万一您想知道,这是规范的一部分,操作员必须输入第三列,而不是简单地在后面的代码中将前两列相加。这样做是为了确保操作员正确转录信息。

我正在尝试使用 .net 中的自定义验证器来创建此客户端验证。但我找不到将三个文本框的名称传递给它的方法。 我可以使用 ControlToValidate 参数为其提供目标控件名称,但如何传入其他两个控件 id ?

我正在寻找“正确”的方法来执行此操作,一种想法是在 JavaScript 中创建一个由 controltovalidate 名称引用的数组。

直流

I have a gridview with three columns of textboxes. It can have as many rows as necessary but its usually only about 5 rows. Each row needs to be validated.

I want to create a client side validator that sums 2 of the columns together and compares it with the third column to check that the user has entered the data correctly.

Just in case you are wondering, it's part of the spec that the operator must enter the third column rather than simply summing the two previous columns together in the code behind. This is done to ensure the operator is transcribing the information correctly.

I am trying to use the custom validator in .net to create this client side validation. but I can't find a way to pass to it the names of the three text boxes.
I can give it the target controls name using the ControlToValidate parameter, but how do I pass in the other two control id's ?

I am looking for the 'proper' way to do this, one thought is to create an array in javascript referenced by the controltovalidate's name.

DC

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2024-08-27 17:37:14

我解决了这个问题。这不是一个优雅的解决方案,但它有效。

首先我将代码放入页面上的 div

<div align="right"><asp:CustomValidator ID="RowValidator" runat="server"
ErrorMessage="Total of #total# does not equal 1st Preference + Ticket"
ControlToValidate="Total" ValidateEmptyText="True" 
ClientValidationFunction="CheckRow" SetFocusOnError="True" EnableClientScript="True"
enableViewState="False" Display="Dynamic"></asp:CustomValidator></div>

然后我创建了一个 JavaScript 函数...

function CheckRow(sender,args) {
// get the name of the control to validate
try {
    args.IsValid = true;
    ctv = sender.controltovalidate;

// get the data from the other controls
    nt = document.getElementById(ctv.replace('_Total','_NonTicket'));
    t = document.getElementById(ctv.replace('_Total','_Ticket'));

    if (nt && t) {
        v1 = Number(nt.value);
        v2 = Number(t.value);
        v3 = Number(args.Value);
        if ((v1 + v2) != v3){
            msg = GetMessage(sender);
            sender.innerHTML = msg.replace("#total#",Number(args.Value));
            args.IsValid = false;
            return false;
        }
   }
}
catch (e) {
    // something wrong default to server side validation
}
return true;
}

这是由每行的自定义验证器调用的我使用发送者的 controltovalidate 参数来获取名称

然后它是一个问题字符串操作来获取其他字段的名称。

一旦检索到,您就可以做您喜欢做的事情,在我的例子中,我添加并比较。如果出现错误,Isvalid 标志将被清除,并且消息将被修改以适应。

getmessage 函数是必需的,因为我更改消息以提供更有意义的错误消息

/*
get the error message from the validator
store it so it can be retrieved again
this is done because the message is altered
*/
function GetMessage(sender){        

msg = window[sender.id+"_msg"];
if (!msg){
    msg = sender.innerHTML;
    window[sender.id+"_msg"] = msg;
}
return msg;
}

getmessage 函数保留原始消息的副本,因此如果用户多次犯错,则可以以其原始形式检索消息,否则第一次编辑消息时,我们会覆盖占位符 (#total#)。

直流

I solved the problem. not an elegant solution but it works.

first I placed the code into a div on the page

<div align="right"><asp:CustomValidator ID="RowValidator" runat="server"
ErrorMessage="Total of #total# does not equal 1st Preference + Ticket"
ControlToValidate="Total" ValidateEmptyText="True" 
ClientValidationFunction="CheckRow" SetFocusOnError="True" EnableClientScript="True"
enableViewState="False" Display="Dynamic"></asp:CustomValidator></div>

Then I created a JavaScript function...

function CheckRow(sender,args) {
// get the name of the control to validate
try {
    args.IsValid = true;
    ctv = sender.controltovalidate;

// get the data from the other controls
    nt = document.getElementById(ctv.replace('_Total','_NonTicket'));
    t = document.getElementById(ctv.replace('_Total','_Ticket'));

    if (nt && t) {
        v1 = Number(nt.value);
        v2 = Number(t.value);
        v3 = Number(args.Value);
        if ((v1 + v2) != v3){
            msg = GetMessage(sender);
            sender.innerHTML = msg.replace("#total#",Number(args.Value));
            args.IsValid = false;
            return false;
        }
   }
}
catch (e) {
    // something wrong default to server side validation
}
return true;
}

This is called by the custom validator for each row I use the controltovalidate parameter of the sender to get the name

then its a matter of a bit of string manipulation to get the names of the other fields.

Once retrieved you can do what you like, in my case I add and compare. if there is an error the Isvalid flag is cleared and the message is modified to suit.

The getmessage function is required because I alter the message to give a more meaningful error message

/*
get the error message from the validator
store it so it can be retrieved again
this is done because the message is altered
*/
function GetMessage(sender){        

msg = window[sender.id+"_msg"];
if (!msg){
    msg = sender.innerHTML;
    window[sender.id+"_msg"] = msg;
}
return msg;
}

The getmessage function keeps a copy of the original message so if the user makes a mistake more than once the message can be retrieved in its pristine form, other wise the first time we edit a message we overwrite the placeholder (#total#).

DC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文