I have a req to provide US Zip+4 with the +4 being optional and the +4 can't be 0000. I'm doing this in .NET therefore I'm using RegularExpressionValidator with RegEx set. In my first validator I'm checking if the Zip code is xxxxx-xxxx or xxxxx format that is 5+4 or 5. In my 2nd validator I check if the last 4 are not set to 0000. This means 1234-0000 is invalid. These are my Regex and I want to be sure they are valid. Seems they test okay, however when cross checking them with the regex101 app online I'm getting different behavior than .NET.
xxxxx-xxxx or xxxxx = ^[0-9]{5}(?:-[0-9]{4})?$
xxxxx-0000 = \d{5}(?!-0000).*
This last one I quite don't understand how it works, but it seems to work. Someone help explain me the ?! and .* they both seem to need to be necessary for this to function. My understanding is the .* means all char and the ?! means negative lookahead????
Actually, the regex pattern I would suggest here is actually a combination of the two you provided above:
^[0-9]{5}(?!-0000$)(?:-[0-9]{4})?$
Demo
Here is an explanation of the pattern:
^ from the start of the ZIP code
[0-9]{5} match a 5 digit ZIP code
(?!-0000$) then assert that the PO box is NOT -0000
(?:-[0-9]{4})? match an optional -xxxx PO box (which can't be 0000)
$ end of the ZIP code
Of note, the (?!-0000$) term is called a negative lookahead, because it looks ahead in the input and asserts that what follows is not -0000. But, using a lookahead does not advance the pattern, so after completing the negative assertion, the pattern continues trying to match an optional -xxxx PO box following.
Related
Hi I'm creating a registration page. It has "Enter License Number:" i want to create a validation expression that if the user type a wrong format in that field. The form will not be submitted. It must be corrected before they submitted. I dragged the "Regular Expression Validator" in my website. But they don't have a default expression for license number. I must custom the expression to have my own expression.
Now i only want to know what is the validation expression of this sample license number:
G11-11-004064 -- A Philippines sample driver's license.
LetterNumberNumber - NumberNumber - NumberNumberNumberNumberNumberNumber
Could you convert it?
Here's a regular expression editor. It's aimed towards Ruby but will do for .NET as well:
http://rubular.com/
I don't know about the detailed specification of the license numbers you#re looking for, but I created a regex based on your example: ^[A-Z]\d{2}-\d{2}-\d{6}$.
You can modify it here:
http://rubular.com/r/7bHsX1tJ23
The example explained:
^[A-Z]\d{2}-\d{2}-\d{6}$
^ = start of line
[A-Z] = a single upper case letter
\d{2} = any number with 2 digits
\d{6} = any number with 6 digits
$ = end of line
If you want to make sure you don't miss lower case letters starting the license use [A-Za-z] instead of [A-Z]
(Thanks to Paul Sullivan)
/[A-Za-z][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9]/
I'm sure this is as basic as it gets but it will match
see online regex tested
In my requirement a Textbox should allow Alphabets,Numeric s, Special Characters,Special Symbols With at least one Alphabet.
I will try like this but i am not getting.
^\d*[a-zA-Z][a-zA-Z0-9#*,$._&% -!><^#]*$
You may want to have 2 regular expression validators; one for validating the allowed characters, and one for validating that at least on alphabet has been provided. You may be able to get at least one, but this way, you can have two separate validation messages to show the user explaining why the input is wrong.
Just match for special characters until you encounter a letter, then match for everything until the end of the string:
^[0-9#*,$._&% -!><^#]*[a-zA-Z0-9#*,$._&% -!><^#]*$
Use lookaheads :
/^(?=.*[a-zA-Z])[\w#*,$.&%!><^#-]*$/
Edit :
I assume the - is meant as the actual - character and not a range of space to !.
I removed the space character. You can of course add it if you want.
[ -!]
Effectively means :
[ -!] # Match a single character in the range between “ ” and “!”
And I have no idea what that range entails!
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!
Using ASP.NET syntax for the RegularExpressionValidator control, how do you specify restriction of two consecutive characters, say character 'x'?
You can provide a regex like the following:
(\\w)\\1+
(\\w) will match any word character, and \\1+ will match whatever character was matched with (\\w).
I do not have access to asp.net at the moment, but take this console app as an example:
Console.WriteLine(regex.IsMatch("hello") ? "Not valid" : "Valid"); // Hello contains to consecutive l:s, hence not valid
Console.WriteLine(regex.IsMatch("Bar") ? "Not valid" : "Valid"); // Bar does not contain any consecutive characters, so it's valid
Alexn is right, this is the way you match consecutive characters with a regex, i.e. (a)\1 matches aa.
However, I think this is a case of everything looking like a nail when you're holding a hammer. I would not use regex to validate this input. Rather, I suggest validating this in code (just looping through the string, comparing str[i] and str[i-1], checking for this condition).
This should work:
^((?<char>\w)(?!\k<char>))*$
It matches abc, but not abbc.
The key is to use so called "zero-width negative lookahead assertion" (syntax: (?! subexpression)).
Here we make sure that a group matched with (?<char>\w) is not followed by itself (expressed with (?!\k<char>)).
Note that \w can be replaced with any valid set of characters (\w does not match white-spaces characters).
You can also do it without named group (note that the referenced group has number 2):
^((\w)(?!\2))*$
And its important to start with ^ and end with $ to match the whole text.
If you want to only exclude text with consecutive x characters, you may use this
^((?<char>x)(?!\k<char>)|[^x\W])*$
or without backreferences
^(x(?!x)|[^x\W])*$
All syntax elements for .NET Framework Regular Expressions are explained here.
You can use a regex to validate what's wrong as well as what's right of course. The regex (.)\1 will match any two consecutive characters, so you can just reject any input that gives an IsValid result to that. If this is the only validation you need, I think this way is far easier than trying to come up with a regex to validate correct input instead.
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}$"