I have 3 filter textboxes on my asp.net page. Each textbox is assigned 2 regularexpressionvalidators to them, 1 for numeric and 1 for alpha. It detects the validations correctly but when i press my search button, it still postback even when the validation tooltip appears(showing the filter is invalid). Any ideas why its "ignoring" the validation and posting back anyways? Here is my definition of the 3 controls and their validations.
<td>
<asp:TextBox ID="txtAcctFilter" runat="server" BackColor="#FF6600" TabIndex="1">Enter Account No</asp:TextBox>
<asp:RegularExpressionValidator ID="revAcctFilter" runat="server" Text="*" ControlToValidate="txtAcctFilter"
ToolTip="Enter Valid Account" ValidationExpression="^\d*$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="revAcctFilter2" runat="server" ControlToValidate="txtAcctFilter"
ValidationExpression="^Enter Account No$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="txtYearFilter" runat="server" BackColor="#FF6600" TabIndex="2"> Enter Year</asp:TextBox>
<asp:RegularExpressionValidator ID="RevYearFilter" runat="server" Text="*" ControlToValidate="txtYearFilter"
ToolTip="Enter Valid Year" ValidationExpression="^20\d{2}$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RevYearFilter2" runat="server" ControlToValidate="txtYearFilter"
ValidationExpression="^Enter Year$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
<td>
<asp:TextBox ID="txtMonthFilter" runat="server" BackColor="#FF6600" TabIndex="3">Enter Month</asp:TextBox>
<asp:RegularExpressionValidator ID="RevMonthFilter" runat="server" Text="*" ControlToValidate="txtMonthFilter"
ToolTip="Enter Valid Month" ValidationExpression="^(1[0-2]|0?[1-9])$" ValidationGroup="Numeric"></asp:RegularExpressionValidator>
<asp:RegularExpressionValidator ID="RevMonthFilter2" runat="server" ControlToValidate="txtMonthFilter"
ValidationExpression="^Enter Month$" ValidationGroup="Alpha"></asp:RegularExpressionValidator>
</td>
This can happen if CausesValidation is not set on your search button. You might check that.
Incidentally, if you have two contradictory regular expressions that you want the content of each textbox to meet, you might not ever be able to submit, because both can't be satisfied at the same time (except by an empty string).
ValidationExpression="^Enter Account No$|^\d+$" is the correct answer for this. I had to use the ^ $ tags for each side of the | to make it validate both sides. This allowed me to go back to just one expression. I also added the causesvalidation for each control to prevent a postback. So far its working :)
Related
I have a field that required 3 different validators:
RequiredFieldValidator, RegularExpressionValidator, and CompareValidator.
Each validator has a "*" to show that something is wrong with the input:
<asp:TextBox ID="txtAddMin" runat="server" Width="30%" MaxLength="2"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Min Value is missing" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator8" runat="server" ErrorMessage="Invalid Min Value Entered" ControlToValidate="txtAddMin" ForeColor="Red" ValidationGroup="vgBinAdd" ValidationExpression="^[0-9]{2}$">*</asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtAddMin" ControlToCompare="txtAddMax" Operator="LessThan" Type="Integer" ErrorMessage="Min Value shoule be less then MaxValue" ForeColor="Red" ValidationGroup="vgBinAdd">*</asp:CompareValidator>
Validations work as expected, however, when failing, the "" is displayed in different positions. So, when CompareValidator fails, the "" is displayed a few spaces from the input field.
Basically, every validator takes up some space after the input field and "*" is displayed with extra space before it based which validator fails.
IS that possible to fix that, so, the start is displayed in the fix position no matter which validator fails?
I have a required field validator and a range validator for a cell in my grid as follows :
<asp:TextBox runat="server" ID="txtItemQuantity" Width="75%" TextMode="Number"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="ReqValItemQuantity" ControlToValidate="txtItemQuantity" ErrorMessage="Invalid Quantity" Text="*" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RanValQuantity" runat="server" ControlToValidate="txtItemQuantity" MinimumValue="0" MaximumValue="99" ErrorMessage="Invalid Quantity" Text="Invalid Quantity"></asp:RangeValidator>
The problem is that this leaves up a very large empty space in my grid for each row which is not convinient:
My questions is, is there a different way of displaying an error message without letting it mess up the grid design ? I thought of something similar to the TextMode error style of the TextBox like below but i'm not sure how to perform this ?
Ok so I have a regular expression validator on my form but I am new to it and was wondering how I add another expression to it so I can look for something else as well. Here is my sample code:
<asp:RegularExpressionValidator
ID="RegularExpressionValidator3"
ControlToValidate="txtRelationship"
ValidationExpression="^[a-zA-Z''-,\'.'\s]{1,30}$"
EnableClientScript="true"
Display="None"
ValidationGroup="<%# ((TSAPassenger)((RepeaterItem)Container.Parent.Parent).DataItem).PaxKey %>"
runat="server"
ErrorMessage="Invalid Relationship.">
Invalid Relationship
</asp:RegularExpressionValidator>
I am trying to add in if the field name is equal to "test" or "tba" to give the same error or a different error. How would I add another expression into this?
Would the second validation look like this?
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtName" ValidationExpression="^.*\b(test|tba)\b.*$"
EnableClientScript="true" Display="None" ValidationGroup="<%# ((TSAPassenger)((RepeaterItem) Container.Parent.Parent).DataItem).PaxKey %>" runat="server" ErrorMessage="This can not be used as a Contact Name."></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" ControlToValidate="txtName" Enabled="<%# (Container.ItemIndex == 0 || Container. = "test") %>" ValidationGroup="<%# ((TSAPassenger)((RepeaterItem) Container.Parent.Parent).DataItem).PaxKey %>" runat="server" ErrorMessage="Contact Name is required."></asp:RequiredFieldValidator>
The best solution would be IMHO to use a custom validator and code your validations in the onServerValidate event.
Of course, you can always add another regExp validator or even a compare validator to the same object you are validating.
Hope this helps
My textbox should contain 8 digits. If not i want to pop up an alert box. But the regualr expression validator does not trigger. Why?
<asp:LinkButton ID="LinkPayment" CssClass="btn mainBtn" runat="server" onclick="LinkPayment_Click" OnClientClick="_gaq.push(['_trackPageview', '/virtualgoal/smspayment']);">Betal ></asp:LinkButton>
<asp:RequiredFieldValidator ID="RequiredCustomersPhone" Display="None" ValidationGroup="ValGroupCustomersPhone" ControlToValidate="CustomersPhone" runat="server" ErrorMessage="Du må fylle inn ditt mobilnummer!"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExValCustomersPhone" ValidationGroup="ValGroupCustomersPhone" runat="server" ControlToValidate="CustomersPhone" Display="None" ErrorMessage="Telefonnummer må bestå av 8 siffer!" ValidationExpression="[0-9]{8}"></asp:RegularExpressionValidator>
<asp:ValidationSummary ID="ValidationSummary" ShowSummary="False" ValidationGroup="ValGroupCustomersPhone" ShowMessageBox="True" runat="server" />
Add the following code:
ValidationGroup="ValGroupCustomersPhone"
to your linkbutton
Are you trying to validate an empty field? You may need a required field validatior as well. It's a shame there is no 'ValidateEmptyText' field on Regex validators.
If have the below markup.
<asp:checkbox id="chkTVLic" runat="server" text="TV Licence" oncheckedchanged="chkDocs_CheckChanged"
autopostback="true" CausesValidation="false" />
<asp:panel id="pnlTVLic" runat="server" visible="false">
<div class="toggle-item-link1 document-date">
<asp:panel id="pnlTVLicIssueDate" runat="server">
<p>
Please enter the date of issue
</p>
<div class="fm-req">
<asp:textbox id="txtTVLicIssueDate" cssclass="tb size2" runat="server" onblur="return true;"></asp:textbox>
<cc2:calendarextender id="caleTVLicIssueDate" runat="server" targetcontrolid="txtTVLicIssueDate"
popupbuttonid="ibnTVLicIssueDate" popupposition="BottomLeft" animated="true"
format="dd/MM/yyyy">
</cc2:calendarextender>
<asp:imagebutton id="ibnTVLicIssueDate" runat="server" imageurl="../images/img-calendar-day.png"
alternatetext="Calendar" tooltip="Pick Date" cssclass="date-picker" />
<asp:requiredfieldvalidator id="rfvTVLicIssueDate" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
display="Dynamic" errormessage="Required" setfocusonerror="true" validationgroup="TVLic"></asp:requiredfieldvalidator>
<asp:comparevalidator id="cmvTVLicIssueDate" CssClass="error" runat="server" errormessage="Not a valid date"
controltovalidate="txtTVLicIssueDate" operator="DataTypeCheck" type="Date" setfocusonerror="true"
validationgroup="TVLic" display="Dynamic" cultureinvariantvalues="true"></asp:comparevalidator>
<asp:customvalidator id="cuvTVLicIssueDate12Months" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
validationgroup="TVLic" display="Dynamic" onservervalidate="cuvDocIssueDate12Months_ServerValidate"
errormessage="Document must be less than 12 months old."></asp:customvalidator>
</div>
</asp:panel>
<asp:panel id="pnlTVLicApprove" runat="server">
<asp:LinkButton id="lbnTVLicApprove" runat="server" CssClass="screen-hide"
alternatetext="Confirm TV Licence" tooltip="Confirm TV Licence" Text="OK" CausesValidation="false" OnClick="lbnApproveConfirm_Click" />
<asp:imagebutton id="ibnTVLicApprove" runat="server" imageurl="../images/img-accept-doc-off.png"
alternatetext="Approve" tooltip="Approve" cssclass="approval-btn" causesvalidation="true" validationgroup="TVLic" OnMouseDown="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" OnClientClick="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" />
<span class="approval-label">Accept document:</span></asp:panel>
</div>
</asp:panel>
The app is written in c# but i havn't posted any actual code as all the user code related to this markup seems to work fine.
The problem is the CheckBox chkTVLic has causes validation set to false and autopostback set to true. So whatever happens when i check and uncheck the checkbox it should postback. Most of the time this is exactly what it does and the result is to show and hide pnlTVLic when it is checked and unchecked. However if any on the validators within the panel fire, the checkbox does not cause a postback the first time. It will on all subsequent times but never the first. However it should ALWAYS cause a postback. What could be stopping it. Before someone asks there is no use written client side code, everything is pure .net markup and c# code.
I don't see why it shouldn't postback when you check/uncheck the checkbox, but if the only purpose of that checkbox is to hide/unhide a panel, I would rather do it in javascript. Doing a full postback to the server just for hiding some panel seems really bad.
In javascript you can do this to hide the panel:
document.getElementById('<%=pnlTVLic.ClientID%>').display='none';
And this to show it:
document.getElementById('<%=pnlTVLic.ClientID%>').display='block';
It's going to be so much faster and better. Just put a regular checkbox instead of the ASP.NET one and subscribe to the onclick event.
Sorry, one more comment:
I think you are wrong when you say that the checkbox should ALWAYS cause a postback. No, if one of the validators fires inside the panel, the checkbox will not cause a postback until the condition is satisfied.
This is what I did and it worked.
on checkbox onclick event I disabled all the validation controls and immediately did Page_ClientValidate(); and it worked.