Asp.net Range Validator - asp.net

Html (Full Code)
<asp:TextBox ID="txtSasiNo" runat="server" Width="250px" MaxLength="17"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="zorunlu" ValidationGroup="kaskoTeklifSayfasi" Display="Dynamic" ID="rangevalidator1" runat="server" SetFocusOnError="true" ControlToValidate="txtSasiNo" ErrorMessage="Enter number characters only (8 or 17)" ValidationExpression="^\d{8}$|^\d{17}$""></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="rfvSasiNo" SetFocusOnError="true" ValidationGroup="kaskoTeklifSayfasi" runat="server" Display="Dynamic" ControlToValidate="txtSasiNo" CssClass="zorunlu">Please enter Number.</asp:RequiredFieldValidator>
Question:
I am trying to use RangeValidator.
If txtnumber is null or empty and if txtnumber is not 8 characters or not 17 characters.
Only 2 options first option is 8 character second option is 17 character.
txtnumber text character only can be like below
12345678 // This is 8 characters
12345678901234567 // This is 17 characters
What to add to RangeValidator in order to achieve this ?
Thanks.

As described by Ondrej you can use RegularExpressionValidator. To be more specific with the aspx. See the code below:-
UPDATED CODE:
Here you go:-
<asp:TextBox ID="txtnumber" runat="server" Width="250px"></asp:TextBox>
<asp:RegularExpressionValidator Display="Dynamic" ID="regtxt" runat="server" SetFocusOnError="true" ControlToValidate="txtnumber" ErrorMessage="Enter number characters only (8 or 17)"
ValidationExpression="^\d{8}$|^\d{17}$"></asp:RegularExpressionValidator>
Hope it helps

Don't use range validator - use regex validator instead:
<asp:RegularExpressionValidator ValidationExpression="^([0-9]{8})|([0-9]{17})$" ... />

The validators usually ignore empty fields. Notable exception: the RequiredFieldValidator. Add that to guard against empty fields and keep the RangeValidator to force a specific range of values.

Related

Validator in asp.net

i have a textbox that will only accpet numbers, so i have to make validator that accept only number except 0, what's the regular expression i should do, any help please, i have already try this :
<asp:RegularExpressionValidator ID="regValSummary" runat="server"
ControlToValidate="txtSummary" Display="Dynamic" ValidationExpression="[1-9]*"
ValidationGroup="grpLigneComande">
*
</asp:RegularExpressionValidator>
any suggestion please !!
You can use Compare Validator instead of RegularExpressionValidator like
<asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBox1"
ErrorMessage="Must enter positive integers" Operator="GreaterThan" Type="Integer"></asp:CompareValidator>
Hope this will help.
i think a RegularExpressionValidator won't be able to help you. Try exploring MaskedTextBox. C# Numeric Only TextBox Control
Or if you're using Ajax Control Toolkit, try the FilteredTextBoxExtender, using a sample MobileNo textfield which will accept numbers except 0:
<asp:TextBox ID="txtMobileNo" runat="server" CssClass="textBox" Width="200px" MaxLength="15" />
<ajax:FilteredTextBoxExtender runat="server" ID="FilteredTextBoxExtender4" TargetControlID="txtMobileNo" FilterMode="ValidChars" FilterType="Numbers, Custom" ValidChars="+-" InvalidChars="0" />

Dual RegularExpressionValidators on 1 control, still does postback?

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 :)

Asp.net regex validator does not work

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.

Regular expression for date in asp.net?

In web application, i am using regular expression for date like "dd-mm-yyyy" format for this i get the validation but it is not working when we enter date like "12-02-2012" remaining condition it is working fine, can you help me this is my validation expression.
<asp:RegularExpressionValidator ID ="myg" runat ="server" ControlToValidate ="txt" ErrorMessage ="Check"
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[13456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$" >
</asp:RegularExpressionValidator>
I solve the problem,
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[123456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
asp:RegularExpression is not the way to do this.
For validating date using an ASP.NET Validator its ideal to use asp:CompareValidator
Here is a small mock-up
<asp:TextBox ID="txtDatecompleted" runat="server"/>
<asp:CompareValidator ID="dateValidator" runat="server"
ControlToValidate="txtDatecompleted"
ErrorMessage="Please enter a valid date."
Operator="DataTypeCheck"
Type="Date"
ValidationGroup="TestVGroup">
</asp:CompareValidator>
<br />
<asp:Button ID="ClickButton" Text="Click Me" runat="server"
ValidationGroup="TestVGroup"
OnClick="ClickButton_Click" />
This will automatically check for different date formats and leap years
I slove the problem,
ValidationExpression ="^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|
((0[1-9]|[12]\d|30)-(0[123456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])
\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|
((16|[2468][048]|[3579][26])00))))$"

Regular expression validation (should start with character )

In my web application, I have a text box.
The user should enter a value of which the first character is a letter, not a digit, and the remaining characters can be alphanumeric.
How can I write regular expression to do this?
You can use: [A-Za-z]\w* to ensure the first character is a letter and any remaining characters are alphanumeric (optional by using the *)
<asp:RegularExpressionValidator ID="rev" runat="server"
ControlToValidate="txtBox"
ErrorMessage="First character must be a letter!"
ValidationExpression="[A-Za-z]\w*" />
<asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="txtBox"
ErrorMessage="Value can't be empty" />
The RequiredFieldValidator is used in conjunction with the RegularExpressionValidator to prevent blank entries. If that textbox is optional, and only needs to be validated when something is entered, then you don't have to use the RequiredFieldValidator.
<asp:TextBox id="TextBox1" runat="server"/>
<asp:RegularExpressionValidator
ControlToValidate="TextBox1"
ValidationExpression="^[A-Za-z]\w*"
ErrorMessage="Input must start with a letter"
runat="server"/>

Resources