ASP.NET date validator passing when it should not - c#

I'm using a RegularExpressionValidator in ASP.NET. I have these validators for my asp:Textbox control:
<asp:RangeValidator
ID="rvDate"
runat="server"
ControlToValidate="txtCrimeDate"
Type="Date"
ErrorMessage="Must be within latest three months"
Display="Dynamic"
OnInit="RangeValidator_Init">
</asp:RangeValidator>
<asp:RegularExpressionValidator
ID="revDate"
runat="server"
ControlToValidate="txtCrimeDate"
Display="Dynamic"
ErrorMessage="Must be in format YYYY-MM-DD"
ValidationExpression="[0-9]{4}-[0-9]{2}-[0-9]{2}">
</asp:RegularExpressionValidator>
I have set the min and max value of the RangeValidator in the code-behind and it works as intended. But! If I enter something with 2 numbers for the year, like 15-11-28 everything passes, even the other validators for the other controls. Any ideas?

Check the date format (US or otherwise) and 0-9 for the day and month are incorrect eg:
2013-99-99
Look into using the CompareValidator control, this is mainly used for string matching, but can be made to check between two date ranges.

Related

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>

Date validation in asp.net for TO and FROM dates

I have select criterion for the to and from dates in asp.net as below.I want to include validation for date format and also for the comaprision. How can i include below requirements in asp.net validations on sub,it button click?
If user select either TO or FROM date he must select both means TO and FROM also.
If the TO date is less than FROM date error message should be displayed.
To check the format, use CompareValidator
<asp:CompareValidator Operator="DataTypeCheck" Type="Date" ...
To check the end date is after the start, try another CompareValidator (this should work - not tested)
<asp:CompareValidator Operator="GreaterThan"
ControlToValidate="txtEnd"
ControlToCompare="txtStart"
Type="Date"
To check your more complex requirements, use the CustomValidator
<asp:CustomValidator

Why isn't my RangeValidator working?

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>

Categories