We are using a RegEx Validator to validate an input from a textbox.
The current RegEx expression checks if the current number is between the range of 1 - 999.
We just inherited this code and we need to change the range from 999 to just 365. In short, we need to write a regex to check if the input is between 1 - 365.
Of course, we can just use a RangeValidator or a custom validator. But we don't want to do that because we don't want to introduce a "significant" change.
Thanks!
EDIT:
We should also catch this following patterns:
001 for 1
019 for 19
Using regular expressions is certainly not the best way to validate integer ranges, but here you go:
^([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$
[1-9][0-9]? matches 1 - 99
[12][0-9][0-9] matches 100 - 299
3[0-5][0-9] matches 300 - 359
36[0-5] matches 360 - 365
EDIT: With leading zeros (also matches strings like 00000000321):
^0*([1-9][0-9]?|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$
Mind the gap, ehm leap year. :-)
you really don't want to use regex for that, you should just use /^\d{1,3}$/ and validate the number as being 1..365
as for just regex:
SNIP
ferdinands is fine :)
Little addition to Ferdinand's regexp for catching leading zeros:
^([0]{0,2}[1-9]|[0]?[1-9][0-9]|[12][0-9][0-9]|3[0-5][0-9]|36[0-5])$
[0]{0,2}[1-9] - catches 001, 01, 1...
[0]?[1-9][0-9] - catches 010, 10...
Notice, it does not catch 0234 or 0019
Related
I'm trying to build a somewhat REGEX expression of the of only numbers including decimal with a maximum of 3 numbers to the right of the decimal (thousandths) and 50 to the left. Valid entries would like something like these.
1
1.0
.1
1.011
.011
1202938.123
1237923782.0
So far I have ^([0-9]*|\d*\.\d{1}?\d*){1,999}$.. Any help appreciated. Thanks.
I believe this should suffice:
^(?=.)\d{0,50}(?:\.\d{0,3})?$
See the regex demo. Note this will also match 1., if this is undesired change \d{0,3} to \d{1,3}. Similarely, this regex will match .5 (with no integer part), if you dont want this then use \d{1,50} instead of \d{0,50}.
You could try:
^(?=.+)\d{0,50}(?:\.\d{1,3})?$
Demonstration here at regex101.com
Explanation -
^ tells the regex that the match will begin at the start of the string,
\d{0, 50} matches 0 - 50 digits,
(?=.+) is a positive look-ahead, that tells the regex that the matching should only start if the line contains some characters in it (as rightly pointed out in the comments!),
(?:\.\d{1,3})? matches an optional dot (.), followed by 1 - 3 digits,
$ tells the regex that whatever it has matched so far will be followed by the end of the string.
Other way: You can check if the string isn't empty and if the dot is always followed by digits, putting a word-boundary at a strategic place:
^\d{0,50}\.?\b\d{0,3}$
As you can see, all is optional in the pattern except the word-boundary that does the magic.
demo
In a web application, while validating the textbox using a regular expression, I have written the expression to validate only digits not starting with zero, with 3 digits after the decimal points. But if I type only a single digit, it's giving me a message. Can you help me with the regular expression? I'm looking for an expression which would not accept a leading digit of zero and accept only 3 decimals like 12.336, 1.254, 10.20, etc.
Depending on exactly what you want:
This will match numbers not begining with 0 and having exactly 3 decimal
^[1-9]\d*\.\d{3}$
This will match numbers not begining with 0 and having 1 to 3 decimal or none.
^[1-9]\d*(?:\.\d{1,3})?$
This should do the trick:
[1-9]\d*\.?\d{0,3}
If you wish to ignore whitespace, just add \s*:
\s*[1-9]\d*\.?\d{0,3}\s*
BTW, there are ton of visual tools for writing regular expressions – I recommend Expresso.
This is what you want:
^[1-9]\d*.?\d{0,3}$
Note this will also fail if there are spaces at either end of the string, remove the ^ at the start and the $ at the end if this is not as desired.
I'm trying to validate the following in a password field:
-at least 1 alpha
-at least 1 numeric
-at least 1 special (non alphanumeric)
My reg exp is this:
Regex.IsMatch("jpere33z#1?hs", #"^\w*(?=\w*\d)(?=\w*[a-z])(?=\W*)\w*$")
and it says it is not valid. The \W part is what is not working.
Could you please tell me why?
\w*$ will only match letters, numbers, and underscore. This is what you want:
Regex.IsMatch("#1?hsjpere33z", #"^(?=.*?\d)(?=.*?[a-z])(?=.*?\W).*$", RegexOptions.IgnoreCase)
I moved the validation to the left, and added \w* right before the \W.
Edit: Also used .* instead of \w for test lookaheads.
Your regex doesn't allow more then 1 digit.
Your easiest route would probably be to have 3 regex checks, one for the existance of each character type.
This is difficult to do with regex (at least only one). In the regex you are giving the fields an order, so the parser expects them in that order.
One alternative would be to use a choice, but that would make difficult to check that you have one of each of the terms:
[\w|\d|\W]{4,}
If you want to use regex, check three of them:
1) Is there a digit?
2) Is there a character?
3) Is there a special?
If all of them are true.... bingo!
I'm having a hard time trying to create a right regular expression for the RegularExpressionValidator control that allows password to be checked for the following:
- Is greater than seven characters.
- Contains at least one digit.
- Contains at least one special (non-alphanumeric) character.
Cant seem to find any results out there too. Any help would be appreciated! Thanks!
Maybe you will find this article helpful. You may try the following expression
^.*(?=.{8,})(?=.*[\d])(?=.*[\W]).*$
and the breakdown:
(?=.{8,}) - contains at least 8 characters
(?=.*[\d]) - contains at least one digit
(?=.*[\W]) - contains at least one special character
http://msdn.microsoft.com/en-us/library/ms972966.aspx
Search for "Lookaround processing" which is necessary in these examples. You can also test for a range of values by using .{4,8} as in Microsoft's example:
^(?=.*\d).{4,8}$
Try this
((?=.*\d)(?=.*[a-z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{7,20} # length at least 7 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
Use for Strong password with Uppercase, Lowercase, Numbers, Symbols & At least 8 Characters.
//Code for Validation with regular expression in ASP.Net core.
[RegularExpression(#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$")]
Regular expression password validation:
#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$"
I am looking for a Regular expression to match only if a date is in the first 28 days of the month. This is for my validator control in ASP.NET
Don't do this with Regex. Dates are formatted differently in different countries. Use the DateTime.TryParse routine instead:
DateTime parsedDate;
if ( DateTime.TryParse( dateString, out parsedDate) && parsedDate.Day <= 28 )
{
// logic goes here.
}
Regex is nearly the golden hammer of input validation, but in this instance, it's the wrong choice.
I don't think this is a task very well-suited for a regexp.
I'd try and use the library functions (DateTime.Parse for .NET) to parse the date and then check the day component of it. Everything else is duplicating half the library function anyways.
Why not just covert it to a date data type and check the day? Using a regular expression, while it could be done, just makes it overly complicated.
([1-9]|1\d|2[0-8]) // matches 1 to 28 but woudn't allow leading zeros for single digits
(0?[1-9]|1\d|2[0-8]) // matches 1 to 28 and would allow 01, 02,... 09
(where \d matches any digit, use [0-9] if your regex engine doesn't support it.)
See also the question What is the regex pattern for datetime (2008-09-01 12:35:45 ) ?
I would use one of the DateTime.TryParse techniques in conjunction with a CustomValidator