Modify Regular Expression for "dd/MM/yyyy" - c#

I have the regular expression for "dd/MM/yyyy" which works fine,
"^([0]?[1-9]|[1][0-2])[./-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0-9]{4}|[0-9]{2})$"
but I want to modify this so it also accepts 00/MM/2014.
My program interpret this as all the days in a specific month. For example 00/04/2014
means all the dates in month of April.
Can someone tell me what kind of change I need to make to above script to make this happen?
Here is the code which I am using it in :
<asp:TextBox ID="TextBox5" runat="server" MaxLength="1" CssClass="MainContent"
style="text-align:justify" ValidationGroup="MKE" Width="130px" />
<asp:ImageButton ID="ImgBntCalc" runat="server" CausesValidation="False"
ImageUrl="images\calendar-schedulehs.png" />
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server"
AcceptNegative="Left" DisplayMoney="Left" ErrorTooltipEnabled="True"
mask="99/99/9999" MaskType="Date" MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
TargetControlID="TextBox5" />
<asp:MaskedEditValidator ID="MaskedEditValidator2" runat="server"
ControlExtender="MaskedEditExtender2" ControlToValidate="TextBox5"
Display="Dynamic" EmptyValueBlurredText="*" ValidationExpression="^(?:[012]?[0-9]|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:[0-9]{2}){1,2}$"
InvalidValueMessage="Date is invalid" ValidationGroup="MKE1" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy"
PopupButtonID="ImgBntCalc" TargetControlID="TextBox5" />

Well right now it's built for MM/dd/yyyy (the first group can only contain 0-12). But here's one that works for dd/mm/yyyy and allows for a 00 or 0 day:
"^([0]?[0-9]|[12][0-9]|[3][01])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$"

You may also want to check out DateTime.ParseExact (format is described here), in most cases it's the easier way to parse dates...

How about:
dd/mm/yyyy:
^(?:[012]?[0-9]|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:[0-9]{2}){1,2}$
mm/dd/yyyy:
^(?:0?[1-9]|1[0-2])[./-](?:[012]?[0-9]|3[01])[./-](?:[0-9]{2}){1,2}$

I'd submit that there's too much going on in this expression. Subjective, to be sure, but if this were my code I'd make a looser regex and do the validation in code. I find it's way easier to come back six months later and understand that, rather than a complicated regex.
That being said...
^(00|0?[1-9]|[12][0-9]|3[01])[./-]([0]?[1-9]|[12][0-9]|[3][01])[./-]([0-9]{4}|[0-9]{2})$
EDIT
By way of explanation, here's the day portion of the expression, commented:
^(
00 # match the literal string '00'
| 0?[1-9] # or, match 1-9, optionally prefixed with '0'
| [12][0-9] # or, match days 10-29
| 3[01] # or, match days 30 and 31
)
Judging by your comments, it sounds like there's a problem with how you're actually using the regex in code. Can you post the code where you're actually using the expression?

Try this pattern
^([0]?[0-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$
IN ASPX
<asp:TextBox ID="TextBox5" runat="server" MaxLength="1" CssClass="MainContent"
style="text-align:justify" ValidationGroup="MKE" Width="130px" />
<asp:ImageButton ID="ImgBntCalc" runat="server" CausesValidation="False"
ImageUrl="images\calendar-schedulehs.png" />
<asp:MaskedEditExtender ID="MaskedEditExtender2" runat="server"
AcceptNegative="Left" DisplayMoney="Left" ErrorTooltipEnabled="True"
mask="99/99/9999" MaskType="Date" MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus" OnInvalidCssClass="MaskedEditError"
TargetControlID="TextBox5" />
<asp:MaskedEditValidator ID="MaskedEditValidator2" runat="server"
ControlExtender="MaskedEditExtender2" ControlToValidate="TextBox5"
Display="Dynamic" EmptyValueBlurredText="*" ValidationExpression="^([0]?[0-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$"
InvalidValueMessage="Date is invalid" ValidationGroup="MKE1" />
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Format="dd/MM/yyyy"
PopupButtonID="ImgBntCalc"
TargetControlID="TextBox5" />
CHECK HERE DATE VALIDATION

Related

MaskedEditValidator not working on client side

I am trying to enforce 15-minute granularity for time information entered by the user. So, for example, 12:15 PM and 3:45 am and 9:30 A.M. are all acceptable, but 2:35 PM would not be allowed. Server-side validation works, but it would be nice if the user was told their input was invalid when the text box loses focus, before they click the submit button. Here is the code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True" UpdateMode="Always">
<ContentTemplate>
<asp:TextBox ID="txtStartTime" runat="server" AutoPostBack="True" CausesValidation="True"/>
<Ajax:MaskedEditExtender ID="txtStartTime_MaskedEditExtender" runat="server"
TargetControlID="txtStartTime" MaskType="Time" AcceptAMPM="True"
Mask="99:99">
</Ajax:MaskedEditExtender>
<asp:RequiredFieldValidator runat="server" ID="StartTimeRequired"
ValidationGroup="EventAddEditControls" ControlToValidate="txtStartTime"
EnableClientScript="True" SetFocusOnError="True">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Invalid time format." ControlToValidate="txtStartTime"
ValidationGroup="EventAddEditControls" SetFocusOnError="True"
EnableClientScript="True" Text="Invalid time format."
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$" />
<Ajax:MaskedEditValidator ID="MaskedEditValidator1" ControlToValidate="txtStartTime"
ControlExtender="txtStartTime_MaskedEditExtender" IsValidEmpty="False"
ValidationGroup="EventAddEditControls"
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$"
EnableClientScript="True" SetFocusOnError="True" Text="Time format is invalid."
runat="server"></Ajax:MaskedEditValidator>
</ContentTemplate>
</asp:UpdatePanel>
How can I get the MaskedEditExtender to enforce the 15-minute granularity constraint on the client side also (assuming that this is possible)?
This seems overly simple, but it seems to work.
The first thing I did was to comment out the RegularExpressionValidator - you don't really need both this and the MaskedEditValidator. To prove the point, apply the fix below but don't comment out the RegularExpressionValidator. Upon a failed validation you will see both errors.
The last thing was to replace the Text property of the MaskedEditValidator with the InvalidValueMessage property:
<Ajax:MaskedEditValidator ID="MaskedEditValidator1" ControlToValidate="txtStartTime"
ControlExtender="txtStartTime_MaskedEditExtender" IsValidEmpty="False"
ValidationGroup="EventAddEditControls"
ValidationExpression="^([1-9]|0[1-9]|1[012]):(00|15|30|45)\s?[aApP]\.?[mM]\.?$"
EnableClientScript="True" SetFocusOnError="True"
InvalidValueMessage="Time format is invalid." runat="server">
</Ajax:MaskedEditValidator>
This example gave me the hint regarding the correct property to use.
Doing both of these things resulted in the validation occurring when tabbing out of the control. This also indicates that the MaskedEditValidator was validating all along.

Why is my Regular Expression not work in ASP.NET

I want use a RegularExpressionValidator for a date in this form yyyy-mm-dd (example: 2012-11-29) and here is my expresion:
/^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$/
I test it on http://www.quanetic.com/Regex and it works but if I do this in my asp.net application it doesn't work
<tr>
<td>Gültig ab:</td>
<td><asp:TextBox ID="txtVon" runat="server" ></asp:TextBox></td>
<td><asp:ImageButton ID="imgVon" runat="server" ImageUrl="images/Calender.ico" Width="15" Height="15" />
<asp:CalendarExtender runat="server" ID="E_Von" TargetControlID="txtVon" Format="yyyy-MM-dd" PopupButtonID="imgVon"/></td>
<td>
<asp:RequiredFieldValidator ID="ValVon"
runat="server" ForeColor="red"
ErrorMessage="*" ControlToValidate="txtVon"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regVon"
runat="server" ControlToValidate="txtVon"
ErrorMessage="*Format" ForeColor="red"
ValidationExpression="/^(19[789]\d|20[0123]\d)\-(0\d|1[012]|\d)\-(31|30|[012]\d|\d)$/"></asp:RegularExpressionValidator>
</td>
</tr>
Where is the error?
Just remove char "/" in the begining and in the and of the string.
And you will have
ValidationExpression="^(19[789]\d|20[0123]\d)-(0\d|1[012]|\d)-(31|30|[012]\d|\d)$"
I use the following, which works ok.
\A(?:^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$)\Z

can't get an asp:RegularExpressionValidator to work

I'm having an issue getting a regex field validator to work for an asp page i'm trying to update.
Here is the asp:Panel stripped down to the important bits:
<asp:Panel ID="pnlEmailAddressCollection" runat="server">
<div id="POMInput-wrapper">
<div class="POMInput-FieldText">
<span class="POMInput-wrapper-text">Name:</span>
<br />
<span class="POMInput-wrapper-text">Email Address:</span>
<br />
</div>
<div class="POMInput-FieldEntry">
<asp:TextBox ID="txtEmailAddress" name="emailAddress" runat="server" CssClass="textInput"></asp:TextBox>
<asp:TextBox ID="txtUserName" runat="server" name="firstName" CssClass="textInput"></asp:TextBox>
</div>
<asp:RequiredFieldValidator ID="rfvNameValidator" runat="server"
ErrorMessage="Please enter your name"
ControlToValidate="txtUserName"
Display="None" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please enter your email address"
ControlToValidate="txtEmailAddress"
Display="None" />
<asp:RegularExpressionValidator ID="rfvEmailValidator2" runat="server"
ErrorMessage="Please enter a valid email address"
ControlToValidate="txtEmailAddress"
Display="None"
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="true"
ShowSummary="false"
EnableClientScript="true" />
</div>
</asp:Panel>
It is currently failing on any email i put in. The asp:RequiredFieldValidator's work as expected.
I tested the regular expression in a test project and the regex seems good (returns true on valid emails, false on invalid ones). Did I set up the asp:RegularExpressionValidator incorrectly?
You should remove double backslash:
ValidationExpression="^[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
Note that you put two backslashes where you meant only one. If you were to set this expression from code behind, the string you provided is correct. But in aspx you don't have to escape backslash.
At the moment accepted email address would be something like abc#abc{backslash}.com
You can try with this code
ValidationExpression="[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
Nota : you can delete ^ and $ symbols
The regular expression works within .NET (server-side), but is failing due to the client-side JScript implemetation as documented in the Remarks section. To verify this (it passes server-side validation), set the EnableClientScript property on the validator to false.
Then undo that change and verify the regex will pass on the client side. You can use an online tester, if it's easier for you.

Use of a RadInputManager (telerik)

i'm trying to use this control to check a time (HH:MM) a user inserts...
This is what i'm using but not working
<telerik:RadTextBox ID="txtEstimation" runat="server">
</telerik:RadTextBox>
<telerik:RadInputManager ID="RadInputManager1" runat="server">
<telerik:RegExpTextBoxSetting EmptyMessage="EmptyMessage" ValidationExpression="(([01][0-9])|(2[0-3])):[0-5][0-9]"
ErrorMessage="*">
<TargetControls>
<telerik:TargetInput ControlID="txtEstimation" />
</TargetControls>
</telerik:RegExpTextBoxSetting>
</telerik:RadInputManager>
I was inspired by this : http://www.telerik.com/help/aspnet-ajax/input-inputmanager-basics.html
Thanks in advance for your help
The purpose of the RadInputManager is to convert standard controls to RadControls at runtime. Therefore, you should either have the RadInputManager OR the RadTextBox, but you don't need both.
You can achieve the HH:MM functionality you want with either way you choose. For example:
<telerik:RadTextBox ID="RadTextBox1" runat="server">
</telerik:RadTextBox>
<asp:RegularExpressionValidator
id="txtValidator"
runat="server"
Display="Dynamic"
ErrorMessage="Please, enter valid time in HH:MM format."
ValidationExpression="^((?<Hour>[0-9]{1,2})[.:](?=[0-9]{2}))?(?<Minute>[0-9]{1,2})$"
ControlToValidate="RadTextBox1">
</asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
http://www.telerik.com/help/aspnet-ajax/input-textbox-limiting-allowable-values.html

Binding a Java Date Picker to a textbox in formview ASP.NET C#

I'm having some problem binding the value of a date picker to a textbox in formview asp.net. I've tried to a put a date picker in ASP.NET and JavaScript which calls on a class file in calendar.css so far it can display the date but if I tried to insert it to a record it always return null. So how can I bind it so it can add the date value to the record?
Help would be much appreciated.
Thanks in advance ;)
Here's a sample of my code. I want to bind the 'input text' to the 'dateborrowedTextBox'
<InsertItemTemplate>
Book Title:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="booktitleDataSource" DataTextField="booktitle"
DataValueField="bookid" SelectedValue='<%# Bind("bookid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="booktitleDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [bookid], [booktitle] FROM [TblBooks]">
</asp:SqlDataSource>
<br />
Employee PIN:
<asp:TextBox ID="employeeidTextBox" runat="server"
Text='<%# Bind("employeeid") %>' />
<br />
Department:
<asp:TextBox ID="departmentTextBox" runat="server"
Text='<%# Bind("department") %>' />
<br />
Date borrowed:
<%--<asp:TextBox ID="dateborrowedTextBox" runat="server"
Text='<%# Bind("dateborrowed") %>' />--%>
<input type="text" name="dateborrowedTextBox" readonly="readonly" id="dateborrowedTextBox">
<a href="#" onclick="cdp1.showCalendar(this, 'dateborrowedTextBox'); return false;">Date Picker
</a>
<br />
<asp:Button ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:Button ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
The client-side IDs of the ASP.NET controls are not going to be translated as cleanly as you are hoping.
You need to do one of two things, both of which can be found here.
Date Picker
Or, you could add a Link control and set the actions that way (the link I gave you does it with an image control). It's up to you.
Just found my answer. ASP.NET cant read Javascript normally so CT100 and some dollar sign will do the trick ;)
Date Picker

Categories