ASP.NET - 带有动态错误消息的自定义验证器
我目前正在尝试确保在文本框中输入的数字可以被 1.25 或 1.5 整除。我决定将数字修改为 1.25 或 1.5 的方式取决于另一个下拉列表中的内容。例如:如果 DDL 的所选索引为 1,则我 mod by 1.5,如果为 2,则我 mod by 1.25。
但是我需要向用户显示抛出错误的原因。自定义验证器的错误消息需要类似于“Number Must be devisible by 1.25”,反之亦然。
据我所知,代码应该可以工作。但事实并非如此。我在另一个论坛上读到,获取源代码并将 insideText 设置为错误消息应该可以解决问题。但我一定是在某个地方做错了什么。当我单步执行 javascript 函数时,它完美地单步执行。只是没有错误信息。这是我的代码:
<asp:CustomValidator ID="ValidateFinHeight" runat="server" CssClass="NormLabel"
Display="Dynamic"
ControlToValidate="txtFinHeight"
ClientValidationFunction="validateFinHeight"></asp:CustomValidator>
<script type="text/javascript" language="javascript" >
function validateFinHeight(source, arguments)
{
var ddl = document.getElementById('cboTubeDia');
var ddlSelIndex = ddl.selectedIndex
switch(ddlSelIndex)
{
case 0:
arguments.isValid = true;
return;
case 1:
if(arguments.value%1.25 != 0)
{
source.innerText = "Height must be divisibly by 1.25";
arguments.isValid = false;
return;
}
else
{
arguments.isValid = true;
return;
}
case 2:
if(arguments.value%1.5 != 0)
{
source.innerText = "Height must be divisibly by 1.5";
arguments.isValid = false;
return;
}
else
{
arguments.isValid = true;
return;
}
}
}
</script>
I am currently trying to make sure a number entered in a textbox is divisibly by 1.25 or 1.5. The wayI decide weather to mod the number by 1.25 or 1.5 is dependant on what is in another drop down list. For example: If the selected index of a DDL is 1, I mod by 1.5, if it is 2 I mod by 1.25.
However I need to display to the user why the error was thrown. The error message of the custom validator needs to be something like "Number Must be devisible by 1.25" or vice versa.
From what I can tell me code should work. But it doesn't. I read on another forum that taking the source and making the innerText your error message should do the trick. But I must be doing something wrong somewhere. When I step through my javascript function it steps through perfectly. Just no error message. Here is my code:
<asp:CustomValidator ID="ValidateFinHeight" runat="server" CssClass="NormLabel"
Display="Dynamic"
ControlToValidate="txtFinHeight"
ClientValidationFunction="validateFinHeight"></asp:CustomValidator>
<script type="text/javascript" language="javascript" >
function validateFinHeight(source, arguments)
{
var ddl = document.getElementById('cboTubeDia');
var ddlSelIndex = ddl.selectedIndex
switch(ddlSelIndex)
{
case 0:
arguments.isValid = true;
return;
case 1:
if(arguments.value%1.25 != 0)
{
source.innerText = "Height must be divisibly by 1.25";
arguments.isValid = false;
return;
}
else
{
arguments.isValid = true;
return;
}
case 2:
if(arguments.value%1.5 != 0)
{
source.innerText = "Height must be divisibly by 1.5";
arguments.isValid = false;
return;
}
else
{
arguments.isValid = true;
return;
}
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据区分大小写的情况,您的 JavaScript 函数中存在一些小错误(fe
IsValid
和Value
)。我已经对其进行了调试,以查看我必须设置 Error-Span 的哪些属性。对于 Firefox,它是textContent
属性;对于 IE,它是innerText
属性。工作功能(跨浏览器):
There were several minor mistakes in your javascript functions according to case sensitivity(f.e.
IsValid
andValue
). I have debugged it to see what property of the Error-Span i must set. It was thetextContent
attribute for Firefox andinnerText
for IE.The working function(cross browser capable):
抱歉,作为访客无法评论其他答案。我想指出这个问题以及蒂姆斯的回答的一个问题:
使用“参数”作为参数名称可能会导致麻烦(至少我刚刚在 Firefox 20 中经历过),因为它也是一个自动变量,包含传递给函数的所有参数,因此如果参数的命名方式相同,则会出现名称冲突。我建议将参数名称更改为“args”或其他任何名称。
Sorry, can't comment other answers as a guest. I'd like to point out an issue with the Question as well as Tims answer:
Using 'arguments' as a parameter name may lead to trouble (at least I just experienced that in firefox 20) because it is also an auto-variable which contains all arguments that are passed to the function, so there is a name conflict if a parameter is named the same way. I recommend changing the parameter name to 'args' or anything else.