I have a textbox called period which should allow the user only to enter the string as YYMMDD format. YY can be numbers from 0-99, MM from 1-12 and DD from 01-04.
I want to use regular expression to get this done. Please help me with the expression to achieve this.
Try this "^[0-9][0-9][0-1][1-2][0][1-4]$" the error in your expression is that it will accept: 991214 which is incorrect.
\d\d(?:0[1-9]|1[012])(?:0[1-4])
matches 110701
does not match 110705
Related
I have a text box for Registration Number and i want it to use the following format 2013/123456/25. I want to validate for in-correct format using Regular Expression Validator.
The exact format must be first 4 numbers/6 numbers/2 numbers -->(2013/123456/25)
Thank you for your help...
You could use something like this :
^\d{4}/\d{6}/\d{2}$
I need to use the RegularExpressionValidator. It has to check the correct format. I have found this nice working expression:
^(((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))))$
But I need this Expression not for dd/mm/yyyy. My date format should be dd.mm.yyyy.
How to transform this expression?
try it
/^(?:(0[1-9]|1[012])[\. \/.](0[1-9]|[12][0-9]|3[01])[\. \/.](19|20)[0-9]{2})$/
or
^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$
It look like only thing that need to be changes is all \/ to \. so it should be
^(((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))))$
I want to check the date which must be in the format dd-mm-yyyy using a regular expression, and it also must check the leap year dates.
I am using RegularExpressionValidator for checking the date.
try this. It works for me!
ValidationExpression="(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))-(((0[1-9])|(1[0-2]))|([1-9]))-(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)"
Try this regular expression-
^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))$
Got it from Here
This regex also handles leap year:
^(((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))))$
Matches
[29/02/2000], [30/04/2003], [01/01/2003]
Non-Matches
[29/02/2001], [30-04-2003], [1/1/1899]
You can also check this link out: http://www.codeproject.com/KB/aspnet/LengthValidation.aspx
You can javascript to check leap year for more info
isLeap = new Date(year, 1, 29).getMonth() == 1
Regular Expression
^(?:^(?:(?:(?:(?:(?:0?[13578]|1[02])/31)|(?:(?:0?[13-9]|1[0-2])/(?:29|30)))/(?:1[6-9]|[2-9]\d)\d{2})|(?:0?2/29/(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))/(?:0?[1-9]|1\d|2[0-8])/(?:(?:1[6-9]|[2-9]\d)\d{2}))$)$
These allow but do not require a leading zero in single-digit months/days. If you don't want that, replace all instances of 0? with 0.
You could use a CustomValidator and have the client-side validation be simple and on the server-side use a DateTime.TryParse to get a definitive validation. Although I suspect you don't need your code to work all the way to the year 9999 (no, I couldn't immediately see if the supplied regexes work that far into the future).
from Microsoft DN but modified to work with years both 20xx and 19xx to used as DOB
^(((((0[1-9])|(1\d)|(2[0-8]))/((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))/((((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))|(((19[0-9][0-9]))|(29-02-19(([02468][048])|([13579][26]))))))$
for dd/MM/yyyy formate
(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$
I have a string which looks like this 512.3 is there a way to add a trailing zero so it looks like this 512.30
Just to clarify (sorry I didn't know there where different ways etc.)
My string is an amount which is passed so changes all the time I only need the trailing zero on amounts like 512.3, 512.4,512.5 etc. as some of my amounts will pass values like 512.33 and 512.44 and so on
Thanks
Jamie
float.Parse("512.3").ToString("0.00");
It would give the number with two decimal digits.
You're going to want to use String.Format or some derivation thereof, and the format string will look like
myString = String.Format("{0:F2}",myObject);
Also note that format strings can be used in the .ToString("F2") method (notice I included the format string F2 inside there already.
See the MSDN links above for a more thorough and definitive explanation.
If it's VB.NET it seems like the simplest solution would be:
FormatNumber("512.3", 2)
Which would return 512.30
Format(5.12, "0.00") this will format it as two decimals.
You'll want to use String.Format
decimal your_number = 1452.66m;
string str = String.Format("{0:C}", your_number);
Single.Parse("512.3").ToString("0.00") ' VB Version
I have a regular expression validator on a text box to validate that the text entered is a valid date.
See reg ex below:
ValidationExpression="^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"
Now I want to allow the following in the textbox: mm/dd/yyyy
How can I update my regex so that if mm/dd/yyyy is entered it does not throw a validation error?
Thanks in advance.
ValidationExpression="^[0-9m]{1,2}/[0-9d]{1,2}/[0-9y]{4}$"
Basically allows 0-9 or m in the first field, 0-9 or d in the second, 0-9 or y in the third (in regular expression [] brackets contain a list of possible options, - denote ranges of values when placed within brackets).
This is a more accurate way to restrict the Date to a more meaningful format
^[1-12]{1,2}/[1-31]{1,2}/[2000-2050,1900-1999]{4}$
This is still not perfect as this will allow - for instance - a date 02/31/2013.
Just realized this is quiet buggy.