CustomValidator的ServerValidate事件有2个参数:source和args.
他们每个人都指出了什么?请给他们任何描述.
谢谢
我在asp.net的客户端有自定义验证器的问题吗?
这是我的fileupload控件和customvalidator,用于检查上传的文件是否为doc !!!!
<asp:FileUpload Id="fu_1" runat="server" />
asp:CustomValidator ID="cv_fu1" runat="server" ControlToValidate="fu_1" ValidationGroup="submit" ClientValidationFunction="file_upload" text="Pls!!! uploat doc file only"> </asp:CustomValidator>
Run Code Online (Sandbox Code Playgroud)
这是我的javascript函数,用于检查上传文件是否为doc !!!!!!
function file_upload()
{
var file1 = document.getElementById("fu_1").value;
len_file1 = file1.length;
var len1_name = file1.substring(len_file1 - 3,len_file1);
if(len1_name != 'doc')
{
alert("wrong file format");
}
}
Run Code Online (Sandbox Code Playgroud)
我想将错误消息放在自定义验证器中代替警报消息...的javascript ..
像其他验证器的错误消息..我把错误放在验证器的文本属性上面..pls检查自定义验证器属性..我想显示的错误...
我检查所有与此相关的问题..但我找不到我想要..
我被引导到一篇非常好的文章,该文章展示了如何从头到尾创建自定义验证器.我唯一的问题是这只适用于单个字段:http: //haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx
如果我需要在模型中验证2个或更多属性,该怎么办?如何将整个模型传递给Validator?
注意:要清楚,我真的不想在回发后验证整个模型......这会破坏这种方法的目的.
我是rails的新手,我在使用rails的Pragmatic Agile Web开发中跟踪Depot应用程序,我遇到了一个奇怪的问题.
在我的产品模型中,我创建了一个验证器,用于确认图像url字段中要求的图像实际上是作为资产存在的.这是我的产品型号代码.
class Product < ActiveRecord::Base
attr_accessible :description, :image_url, :price, :title
validates :description, :price, :title, :presence => true
validate :image_url_exists, on: :create
def image_url_exists
if Rails.application.assets.find_asset(image_url) == nil
errors.add(:image_url, 'is not valid. The image does not exist.')
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在的问题是我运行我的单元测试.这是它的内容:
require 'test_helper'
class ProductTest < ActiveSupport::TestCase
test "the products attributes should not be empty" do
p = Product.new
assert p.invalid?
end
end
Run Code Online (Sandbox Code Playgroud)
但这样做会在我的代码中触发一堆错误.没有自定义验证器,一切似乎都运行得很好.以下是我目前遇到的错误.
test_the_products_attributes_should_not_be_empty(ProductTest):
TypeError: can't convert nil into String
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `initialize'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `new'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/base.rb:156:in `find_asset'
/var/lib/gems/1.9.1/gems/sprockets-2.2.2/lib/sprockets/index.rb:60:in …
Run Code Online (Sandbox Code Playgroud) 我想从Code Behind获取ControlToValidate属性,这是我的aspx.
<asp:CustomValidator runat="server" ID="custtxtTest" OnServerValidate="custtxtTest_ServerValidate" ControlToValidate="txtTest" ForeColor="Red" Text="*" />
Run Code Online (Sandbox Code Playgroud)
在我的代码后面我想获取属性"ControlToValidate",但这似乎不是源的有效属性:
protected void custtxtTest_ServerValidate(object source, ServerValidateEventArgs args)
{
string test = source.ControlToValidate;
}
Run Code Online (Sandbox Code Playgroud)
我检查了Asp.Net自定义验证器:如何在ClientValidationFunction上获取'controlToValidate'属性?但这只适用于客户端功能,而不是后面的代码.
任何人都可以帮助我在自定义验证器 Parsley JS 中访问 parsleyField.$element 吗?
代码:
window.ParsleyValidator.addValidator('customValidator',
function (value, requirement) {
// how to access here to parsleyField.$element ?? or how to access my custom data-attributes in field ??
return false; //debug
}, 32)
.addMessage('ru', 'customValidator', 'message');
Run Code Online (Sandbox Code Playgroud)
PS Parsley JS 版本 2.1.3
我在Rails中使用自定义验证遇到了一些麻烦。我想验证的是,只有数字被输入到下拉列表中选择拨号的字段中。
到目前为止我的代码
validate :validate_numericality_of_dial_arg
def validate_numericality_of_dial_arg
if action_type == "dial"
return action_dst =~ /^\d+$/
else
errors.add(:base, "must be numbers")
end
end
Run Code Online (Sandbox Code Playgroud)
现在,我弹出错误提示说即使用户输入数字也必须是数字
我在Web表单上使用了一堆不同的asp.net验证控件.其中一些将其Text属性设置为"*Error"或"您错过了这些字段"之类的内容.
但是,一些CustomValidator控件具有空白的Text属性.我故意将它们留空,因为我根据哪种情况失败动态添加ErrorMessage.(CustomValidator可能有许多不同的条件,我设置args.IsValid = false)
发生错误时,我设置的ErrorMessage属性将显示在ValidationSummary和Validator控件内.我不希望这样.我希望能够在ValidationSummary中显示ErrorMessage,而不是在BaseValidator.Text属性中显示.
我的第一次尝试是将Text属性设置为空格"".那没用.
我实现的(现在)是一个显示为背景颜色相同的时期.这是一个黑客 - 我不喜欢它.哎呀,也许这就是我在这里的原因!
这是代码:
<asp:CustomValidator ID="StackOverflowValidator" runat="server"
Text="."
CssClass="validatorstyle"
Display="Dynamic"
OnServerValidate="validate_AllowedToDoSomething"
ValidationGroup="MainGroup" />
<asp:ValidationSummary ID="mainGroupValidationSummary" runat="server"
ValidationGroup="MainGroup"
DisplayMode="BulletList"
HeaderText="There was an error in saving. Please check the following:" />
Run Code Online (Sandbox Code Playgroud)
在validate_AllowedToDoSomething里面我打电话:
StackOverflowValidator.ErrorMessage = "Custom Error Message #1";
args.IsValid = false;
return;
Run Code Online (Sandbox Code Playgroud)
我得到的是"自定义错误消息#1" 两次在Web表单上.提前致谢!
我的 ASP.Net (4.0) 页面中有以下代码,其中有 2 个按钮 - 'Button1' 和 'Button2'、一个文本框、一个必填字段验证器和一个自定义验证器。
我希望自定义验证器仅在单击“Button1”时触发,而不是在单击“Button2”时触发,Button2 仍需要评估所需的字段验证器。我将如何做到这一点?
Input 1:
<asp:TextBox id="txtCustomData" runat="server" />
<asp:CustomValidator id="CustomValidator1"
runat="server" ErrorMessage="Number not divisible by 2!"
ControlToValidate="txtCustomData"
ClientValidationFunction="CheckEven" />
<br/>
Input 2:
<asp:TextBox id="TextBox2" runat="server" />
<asp:RequiredFieldValidator ID="rfv1" runat="server" ControlToValidate="TextBox2"
ErrorMessage="* Required" ForeColor="Red" >
</asp:RequiredFieldValidator>
<br/>
<br/>
<asp:Button id="btn1" runat="server" Text="Button1" />
<br/>
<asp:Button id="btn2" runat="server" Text="Button2" />
<script language="javascript">
<!--
function CheckEven(source, args) {
var val = parseInt(args.Value, 10);
if (isNaN(val)) {
args.IsValid = false;
}
else {
args.IsValid …
Run Code Online (Sandbox Code Playgroud)