Why isn't my RangeValidator working? - c#

I succesfully used a validator more than once,but after some programming my validators are not working.Maybe is something i don't know about defining 2 validators for the same control, but it doesn't work for one validator in a control either.Here are 2 examples of my code:
Example 1: one required field validator and one "maximum value" validator for username:
<asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Dynamic" ErrorMessage="Required" />
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="UsernameTextbox" MinimumValue="1" MaximumValue="20" ForeColor="red"
Display="Dynamic" ErrorMessage="Name must contain maximum 20 characters"></asp:RangeValidator>
Example 2: one "maximum value" validator for roadaddress(string):
<asp:RangeValidator ID="RangeValidator9" runat="server" MaximumValue="50" ForeColor="red"
ErrorMessage="Road Address must contain maxmum 50 characters" ControlToValidate="RoadAddressTextbox"></asp:RangeValidator>
I am thinking that the problem is maybe in the display property or in the causesvalidation property which i don't use...

That's not what the RangeValidator is used for. The RangeValidator is intended to check input to make sure it's within a certain range, i.e. to make sure that a number is between 1 and 5, that a date is within a certain range, etc.
What you need is a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="UserNameTextbox"
ErrorMessage="Username must be between 1 and 50 characters"
ValidationExpression="^[a-zA-Z\s]{1,50}">
</asp:RegularExpressionValidator>
EDIT: Updated expression to ^[a-zA-Z\s]{1,50}

RangeValidators don't validate the number of characters in the input, they "Check whether the value of an input control is within a specified range of values."
You can actually do this without a Validator, by setting the MaxLength property on the text box which would limit the number of characters entered into it.
<asp:TextBox ID="UserNameTextbox" MaxLength="50" runat="server"></asp:TextBox>

Related

MaskedEditExtender not holding Mask after clicking away

I cannot figure out why my one field (Phone Number) isn't holding its MaskedEditExtender after you click away, while the others work just fine. I compared the properties and controls and they're all the same.
I suspect that it has something to do with my Validation Expression not matching my Mask, but when I tried adding in the exceptions for the symbols [() and -], it didn't work (or I did it wrong).
The problem child:
<div class="col-xs-6">
<asp:TextBox
runat="server"
ID="TextBox_PhoneNumber"
class="form-control"
Style="max-width: 145px" />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator_PhoneNumber"
runat="server"
ControlToValidate="TextBox_PhoneNumber"
ErrorMessage="Invalid phone number."
ValidationExpression="^[2-9]\d{2}\d{3}\d{4}$"
Display="Dynamic"
SetFocusOnError="True"
Font-Bold="true"
ForeColor="red"
ValidationGroup="ValidationGroup_Main" />
<asp:RequiredFieldValidator
runat="server"
ID="RequiredFieldValidator_PhoneNumber"
ControlToValidate="TextBox_PhoneNumber"
ErrorMessage="Employee Info: Phone Number is blank."
Text="Required"
class="text-right"
Display="Dynamic"
Font-Bold="true"
ForeColor="red"
ValidationGroup="ValidationGroup_Main" />
<ajaxToolkit:MaskedEditExtender ID="maskededitextender_PhoneNumber" runat="server" TargetControlID="TextBox_PhoneNumber" Mask="(999) 999-9999" MaskType="Number" AcceptNegative="None" AutoComplete="false" />
Resulting in (while entering the phone number):
Resulting in (after clicking away after entry):
How can I get that mask to stick?
Figured it out myself:
In the "MaskedEditExtender", I needed to add this control:
ClearMaskOnLostFocus="false"
This forces the mask to stick at all times. I applied this to all of my other Masks now too (Dates, Zipcode, etc).
The reason my Date fields weren't having this problem is because the "MaskType" of the "MaskedEditExtender" was set to "Date". There isn't one for Phone Number, and therefore it was treating it as either a number or nothing, neither of which have default Phone Number formatting like Date does.
However this did mess up my ValidationExpression in the Regex (because it was counting the Validation Expression as part of the data being submitted, and therefore I needed to add the formatting that my mask was using to my expression), but I just adjusted it to this:
ValidationExpression="^\([2-9]\d{2}\) \d{3}\-\d{4}$"
Success!
Hope this helps somebody else.

How to write a RegularExpressionValidator for a 10 Digits mobile number?

I am a new ASP.NET Webforms developer and I am struggling right now with validating the TextBox control that asks for a mobile phone number. There is only one format used when entering a phone number which is
05# ### ####
I would like to use ASP.NET RegularExpressionValidator control with this TextBox and I need to a regular expression that matches the preceding expressions. The regular expression should validate the entered number against the following rules:
The preceding format
The number must start with 05
The number must be of 10 digits only
So how can I write a regular expression (or ValidationExpression) property for this ASP.NET RegularExpressionValidator control?
Here's a snippet of my work:
<asp:TextBox ID="txtPhoneNew" runat="server" CssClass="form-control" placeholder="966 ## ### ####"></asp:TextBox>
<asp:RegularExpressionValidator ID="revPhoneNew" runat="server"
ControlToValidate="txtPhoneNew" ErrorMessage="Enter a valid mobile phone number"
ValidationExpression="[0-9]{10}"></asp:RegularExpressionValidator>
as Tim stated, and per your example the regular expression you need is
<asp:TextBox ID="txtPhoneNew" runat="server" CssClass="form-control" placeholder="966 ## ### ####"></asp:TextBox>
<asp:RegularExpressionValidator ID="revPhoneNew" runat="server"
ControlToValidate="txtPhoneNew" ErrorMessage="Enter a valid mobile phone number"
ValidationExpression="^05\d{3}\s\d{4}$"></asp:RegularExpressionValidator>
try this
<asp:TextBox ID="txtPhoneNew" MaxLength="10" runat="server" CssClass="form-control" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ControlToValidate="txtPhoneNew"
ErrorMessage="enter valid phone" ValidationExpression="^05[0-9]{8}$"></asp:RegularExpressionValidator>

Place holder text is missing and back space not working in masked text box

I have used the masked textbox in asp.net c# application
<div class="span3">
<asp:TextBox ID="txtExpiryDate" placeholder="(mm-dd-yy)" CssClass="datepiker" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
Display="Dynamic" ControlToValidate="txtExpiryDate"><b>Enter Renewal Date</b></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator Display="Dynamic" ID="valdRegStartDate" runat="server"
ControlToValidate="txtExpiryDate" ErrorMessage="Enter a valid Expiry Date." ValidationExpression="([1-9]|0[1-9]|1[012])([-/.])([1-9]|0[1-9]|[12][0-9]|3[01])([-/.])(19[5-9][0-9]|20[0-4][0-9])"><b>Enter a valid Expiry Date.</b>
</asp:RegularExpressionValidator>
<cc1:MaskedEditExtender ID="MaskDate" Enabled="true" runat="server" MaskType="Date"
Mask="99-99-9999" TargetControlID="txtExpiryDate" MessageValidatorTip="true" ClearMaskOnLostFocus="false">
</cc1:MaskedEditExtender>
</div>
The placeholder (mm-dd-yy) in the textbox is not displayed and backspace is not working.
It displays placeholder as __-__-____ instead of (mm-dd-yy).
Please help me !!!
Remove placeholder in your markup and try this in your Page_Load:
txtExample.Attributes.Add("placeholder","mm/dd/yyyy");
The MaskedEditExtender is overriding your placeholder. That's why you're seeing __-__-____ (according to the mask). You can't really have both so one will have to give. You already have the regular expression validator (which is checking for a 4-digit year) so I suggest removing the MaskedEditExtender and tweaking your validator.
I'd even go as far as saying using a CustomValidator to clean up the markup a little.

Thousand separator with MaskedEditExtender from the ASP.NET AJAX Control Toolkit

I need to format some inputs in an Textbox and I've tried to do so with the MaskedEditExtender. I don't know what mask I have to use to get what I want - maybe somebody knows.
The entered values are only numeric values with amounts between 100 and 9999999 Euros an I want to show the thousand separators an cent separators while typing into the textbox like this:
Input: 100
Show: 100,00
Input: 345000,50
Show: 345.000,50
To be more specific I want the exact same behavior like typing numeric values into an calculator and the kind the calculator shows the entered values on the display.
Here is an (expensive) example with exact the behavior I need for free: https://demos.devexpress.com/ASPxEditorsDemos/Features/MaskedInput.aspx
This is my code:
<asp:TextBox ID="purposeAmount" CssClass="textBoxCreateItem" OnTextChanged="purposeAmount_OnTextChanged" AutoPostBack="true" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="purposeAmount" Display="Dynamic" SetFocusOnError="true" ErrorMessage="Only numeric allowed." ForeColor="Red" ValidationExpression="^\d+(\,\d+$)?$" ValidationGroup="NumericValidate"><br />Erlaubte Zeichen: 0-9 und ,</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" ControlToValidate="purposeAmount" ValidationGroup="NumericValidate" Display="Static" ForeColor="Red"><br />Eingabe erforderlich.</asp:RequiredFieldValidator>
<cc1:MaskedEditExtender TargetControlID="purposeAmount" MaskType="Number" Mask="9,999.99" runat="server" />
Try this:
<ajax:MaskedEditExtender ID="MaskedEditExtender2" Mask="9,999.99"...</ajax:MaskedEditExtender>

How can i prevent only html tags which does not include numbers, so how can i allow numbers

I set validation expression ValidationExpression="<(.|\n)*?>" but it does not give permission to numbers how can i solve it?
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator" ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"></asp:RegularExpressionValidator><br />
Instead of (.|\n)*, use \D*.

Categories