自定义验证器客户端函数
我有一个自定义验证器(.net 3.5),它检查表单中的四个下拉列表是否具有重复值。它可以在服务器端运行,但我想添加一个客户端功能来配合它。我对 JavaScript 毫无了解。你能帮忙吗?曼特谢谢。
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>
Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue)
End Sub
Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
If first <> "" And second <> "" AndAlso first.Equals(second) Then
Return first.Equals(second)
End If
End Function
更新:以下 JavaScript 代码可以正常工作,因为它检查下拉列表中是否存在重复值。但是,我如何将其链接到我的自定义验证器并消除警报消息。按照现在的情况,页面已提交。谢谢。
function dropDownValidation_ClientValidate() {
var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');
var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2');
var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3');
var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4');
var result = haveSameValue(strValue1.value, strValue2.value) &&
haveSameValue(strValue1.value, strValue3.value) &&
haveSameValue(strValue1.value, strValue4.value) &&
haveSameValue(strValue2.value, strValue3.value) &&
haveSameValue(strValue2.value, strValue4.value) &&
haveSameValue(strValue3.value, strValue4.value);
return result;
}
function haveSameValue(ddlValue1, ddlValue2) {
if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
alert("Related documents contain duplicate values");
}
}
I have a custom validator (.net 3.5) that checks if four dropdown lists in my form have repeated values. It works on the server-side but I would like to add a client-side function to go with it. I have no knowledge of JavaScript. Could you help? Mant thanks.
<asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>
Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue)
End Sub
Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
If first <> "" And second <> "" AndAlso first.Equals(second) Then
Return first.Equals(second)
End If
End Function
UPDATE: The following JavaScript code works ok as it checks if there are duplicate values in the dropdown lists. However how can I link this to my custom validator and eliminate the alert message. As it stands now the page gets submitted. Thanks.
function dropDownValidation_ClientValidate() {
var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');
var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2');
var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3');
var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4');
var result = haveSameValue(strValue1.value, strValue2.value) &&
haveSameValue(strValue1.value, strValue3.value) &&
haveSameValue(strValue1.value, strValue4.value) &&
haveSameValue(strValue2.value, strValue3.value) &&
haveSameValue(strValue2.value, strValue4.value) &&
haveSameValue(strValue3.value, strValue4.value);
return result;
}
function haveSameValue(ddlValue1, ddlValue2) {
if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
alert("Related documents contain duplicate values");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在不使用任何库(例如 jQuery)的情况下,最简单的方法可能是这样的:
HTML:
JavaScript validator:
您需要将
validator
连接到按钮,它可以是或
或
或
Without using any libraries (such as jQuery), the simplest way is probably this:
HTML:
JavaScript validator:
You need to wire up
validator
to the button, which could be a<input type="submit/image/etc" />
or a<button>
or several other things. Whatever it is, you can just bind theonclick
event to the element. E.g.:Or
Or
我认为你需要这样的东西:
你能检查一下它是否有效吗?
You need this kind of thing I think:
Can you check if it works?
这是旧的,但我想在这里给出一个答案,因为我是在谷歌搜索类似问题时得到的。
在您的 Javascript 函数中,它将采用 2 个参数:source 和 args。您需要根据验证是否成功将 args.IsValid 设置为 true 或 false。如果您正在验证特定字段,则 args.Value 将是正在验证的表单字段的值。
一个简单的验证器函数来检查表单是否等于“1”,如下所示:
将其与上面的答案结合起来并设置 args.IsValid 而不是返回值,您应该得到答案。
更多信息可以在此中找到4guysfromrolla.com 文章。
This is old, but I wanted to put an answer here, as I got it in response to a Google search for a similar question.
In your Javascript function, it will take 2 parameters, source and args. You want to set args.IsValid to either true or false depending on whether or not validation succeeded or not. args.Value will be the value of the form field being validated if you are validating a specific field.
A simple validator function to check if the form was equal to "1" would look like this:
Combine this with the above answers and set args.IsValid rather than the return, and you should have your answer.
More information can be found in this 4guysfromrolla.com article.