Richard
Richard

Reputation: 137

Asp.net Range Validator

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.

Upvotes: 0

Views: 688

Answers (3)

HEEN
HEEN

Reputation: 4721

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

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39274

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.

Upvotes: 0

Ondrej Svejdar
Ondrej Svejdar

Reputation: 22054

Don't use range validator - use regex validator instead:

<asp:RegularExpressionValidator ValidationExpression="^([0-9]{8})|([0-9]{17})$" ... />

Upvotes: 1

Related Questions