我在asp.net中有下面的代码片段,其中我正在验证一个Textbox.I,我添加了两个验证控件,第一个验证日期的格式,第二个验证未来的数据。
现在的问题是,两个验证同时开火。我要先检查日期是否有效,然后我要解雇牧羊人。
<asp:TextBox Enabled="True" runat="server" size="8" MaxLength="10" meta:resourcekey="txtTravelerDOBResource2">mm/dd/yyyy</asp:TextBox>
<asp:RangeValidator ID="rangeValidator" ControlToValidate="txtTravelerDOB" MaximumValue="09/25/2013" MinimumValue="1/1/2012" Type="Date" ErrorMessage="Future Date Not allowed" runat="server"></asp:RangeValidator>
<asp:RegularExpressionValidator Enabled="True" ID="rgxDOB" runat="server" ControlToValidate="txtTravelerDOB"
Display="Dynamic" ErrorMessage="Date is not valid"
ValidationExpression="^(((0?[13578]|1[02])[\/](0?[1-9]|[12]\d|3[01])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[13456789]|1[012])[\/](0?[1-9]|[12]\d|30)[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/](0?[1-9]|1\d|2[0-8])[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/]29[\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$"
></asp:RegularExpressionValidator>我试图使用javascript禁用验证控件,如下所示。
function isGoodDate(){
var value=$("#ctl09_ctl00_ctl00_ctl00_rptTravelers_ctl01_txtTravelerDOB").val();
var v=$("#ctl09_ctl00_ctl00_ctl00_rptTravelers_ctl02_txtTravelerDOB").val();
var reGoodDate = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
console.log(value);
if(reGoodDate.test(value))
{
$.each(Page_Validators, function (index, validator){
if (validator.validationGroup == "x"){
ValidatorEnable(validator, true);
}
});
}
else
{
ValidatorEnable(validator, false);
}
if(reGoodDate.test(v))
{
$.each(Page_Validators, function (index, validator){
if (validator.validationGroup == "y"){
ValidatorEnable(validator, true);
}
});
}
else
{
ValidatorEnable(validator, false);
}
}发布于 2013-09-26 09:07:04
首先,所有的验证器不完全同时开火。它们看起来就像在几秒钟内发生的那样。
您在.aspx页面中添加的验证器按照创建/添加到页面的相同顺序添加到Page.Validators集合中。验证按照它们在Page.Validators collection.Thus中存在的顺序运行,aspx文件中的第一个验证器在Page.Validators中第一个。如果您想要重新排列订单,那么正确的方法是将您的验证器按您希望它们触发的顺序排列在页面中。
注意事项:验证器将一个一个地启动。如果您不希望触发下一个验证器,您可以使用Javascript禁用下一个验证器。在第一个验证器中调用ClientValidation函数
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox3"
ClientValidationFunction="disableNextVal" .... />//示例JavaScript代码
function disableNextVal()
{
// firstly check here for first condition, if First condition fails,
// disable the next validator as below.
var nextVal = document.getElementById('nextValidatorClientID');
ValidatorEnable(myVal, false);
// or use this one:
myVal.enabled = false;
}但是,下面还提到了一个更好的解决方案。
在这些情况下,在TextBox中输入的值应该通过多个条件,例如:数据格式,值应该大于某些最低要求值等等。使用http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx控件总是很好的。
在这个自定义验证器控件中,每个条件都一个一个地检查。如果第一个条件失败:日期无效,请不要检查其他条件,只显示第一个错误消息。类似地,如果第二个条件失败:范围无效,则只显示第二个失败条件的消息。
<asp:CustomValidator ID= "valxTextBox" runat="server"Enabled="true"
ControlToValidate="txtDate"
ClientValidationFunction ="ValidateTxtDate"
ValidateEmptyText="true"
OnServerValidate="valxTextBox_ValidatePostalCode"
></asp:CustomValidator>正如您所看到的,这提供了为验证定义自定义客户端和服务器端事件的灵活性。
在您的服务器验证中,逐个检查条件,并在发现一个失败时立即返回。
使用System.Text.RegularExpressions 命名空间的 Regex 类对regularExpressions进行数据验证。
protected void valeEmailAddress_txtEmailAddressValidate(object sender,
ServerValidateEventArgs e)
{
string MaximumValue="09/25/2013";
string MinimumValue="1/1/2012";
// check for first condition
if(txtTravelerDOB >MaximumValue ||txtTravelerDOB < MinimumValue )
{
// sample code here
// if failing, set IsValid to false
e.IsValid = false;
// dynamically set the error message as per first condition
valxTextBox.ErrorMessage ="Not a valid date";
}
// check for second condition
// read the expression pattern from appSettings
if(!Regex.Match(txtTravelerDOB.Text.Trim(),
WebConfigurationManager.AppSettings("travelerDOBRegEX")).Success)
{
// if fails,
e.IsValid = false;
valxTextBox.ErrorMessage ="Format is Invalid";
}
}应用程序值:
<add key="travelerDOBRegEX" value="^(((0?[13578]|1[02])[\/](0?[1-9]|[12]\d|3[01])[\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[13456789]|1[012])[\/](0?[1-9]|[12]\d|30)[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/](0?[1-9]|1\d|2[0-8])[\/]((1[6-9]|[2-9]\d)?\d{2}))|(0?2[\/]29[\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))$"/>https://stackoverflow.com/questions/19021807
复制相似问题