Some users a writing their messages in uppercase only, and I want to avoid that with JQuery Validation Engine.
I have tried many many regex without any success.
Here is the idea for a custom rule to avoid more than 10 uppercase characters:
uppercase: {
regex: /^(![A-Z]{10})+$/,
alertText: "* uppercase test alert"
},
I can't figure out what's wrong.
If you want to only allow strings with 10 and fewer uppercase letters, you may use
/^(?!(?:[^A-Z]*[A-Z]){11})/
See the regex demo
The pattern matches any string that does not contain 11 or more ASCII uppercase letters (so, it may contain 0 to 10 ASCII uppercase letters).
Details
^ - start of string
(?!(?:[^A-Z]*[A-Z]){11}) - a negative lookahead that fails the match if, immediately to the right of the current position, there are
(?:[^A-Z]*[A-Z]){11} - 11 occurrences of
[^A-Z]* - any 0+ chars other than uppercase ASCII letters
[A-Z] - an uppercase ASCII letter.
If you want to match a string that has no 10 uppercase ASCII letters on end:
/^(?!.*[A-Z]{11})/
See the regex demo.
Details
^ - start of the string
(?!.*[A-Z]{11}) - a negative lookahead that fails the math if there are 11 uppercase ASCII letters after any 0+ chars other than line break chars immediately the right of the current location.
Related
I am new to regular expressions. I want to write a regex to detect two lowercase letters in a String. The String could contain any number of UpperCase letters, digits and symbols. Just want to make sure that the String contains two lowercase letters for sure using the regex expression.
I tried using
RegExp(r'[a-zA-Z]{2}')
But it returns true even when the String contains 1 lowercase and 1 uppercase. I want to check if the String contains at least 2 lowercase letters.
Could someone help?
You can use a regex like
(?:[^a-z]*[a-z]){2}
See the regex demo. Details:
(?: - start of a non-capturing group:
[^a-z]* - zero or more chars other than lowercase ASCII letters
[a-z] - a lowercase ASCII letter
){2} - end of the grouping construct, match two repetitions
In Dart, you can use
int n = 2;
RegExp rex = RegExp("(?:[^a-z]*[a-z]){$n}");
print(rex.hasMatch("Food")); // => true
I am writing an expression to validate a username with the following requirements:
length between 6 and 20,
must start with a letter,
numbers are allowed,
dot is optional or only one is allowed.
must not end with a dot
Tried with but didn't work:
^(([a-zA-Z])(\.{0,1})([a-zA-Z0-9]*)){6,20}$
another option
^([a-zA-Z]+([\\.]?)+([a-zA-Z0-9]*)){6,20}$
You may use an expression that will match a letter at the start and the 5 to 19 letters, digits or dots that should only appear in the middle of the string.
You may use
^(?=.{5,19}$)(?=[A-Za-z])[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)*$
See the regex demo.
Details
^ - start of string
(?=.{5,19}$) - a positive lookahead that matches a string that contains any five to nineteen characters other than line break chars
(?=[A-Za-z]) - the first char must be an ASCII letter
[A-Za-z0-9]+ - one or more letters or digits
(?:\.[A-Za-z0-9]+)* - 0 or more repetitions of
\. - a dot
[A-Za-z0-9]+ - one or more letters or digits
$ - end of string.
Building a regex to match passwords stored in plain text.
8-15 characters, must contain at least:
1 uppercase letter [A-Z]
1 lowercase letter [a-z]
1 number \d
1 special character [!##\$%\^&\*]
The problem I have is when the password is inline with other text or spaces after, it doesn't return a match. When it's on its own without spaces it matches.
Example:
This is a Testing!23 surrounded by other text.
Testing!23
(?=.{8,15})(?=.*[!##\$%\^&\*])(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*
You want to find all non-whitespace chunks matching the conditions you outlined.
Use
(?<!\S)(?=\S{8,15}(?!\S))(?=[^!##$%^&*\s]*[!##$%^&*])(?=[^\s\d]*\d)(?=[^\sa-z]*[a-z])(?=[^\sA-Z]*[A-Z])\S+
See the regex demo
Details
(?<!\S) - a whitespace or start of string should be right before the current position
(?=\S{8,15}(?!\S)) - right after the current position, there must be 8 to 15 non-whitespace chars followed with either whitespace or end of string
(?=[^!##$%^&*\s]*[!##$%^&*]) - there must be a char from the [!##$%^&*] set after zero or more non-whitespace chars outside of the set
(?=[^\s\d]*\d) - there must be a digit after 0+ non-whitespace and non-digit chars
(?=[^\sa-z]*[a-z]) - 1 lowercase letter must appear after 0+ chars other than whitespace and lowercase letters
(?=[^\sA-Z]*[A-Z]) - 1 uppercase letter must appear after 0+ chars other than whitespace and uppercase letters
\S+ - all checks are over, and if they succeed, match and consume 1+ non-whitespace chars (finally).
I would like to match String that contains letters and numbers WITHOUT space
I tried ^[a-zA-Z+d*]*$ but it matches String that have only letters
This is what it should do :
Nope
Nope 2
MatchPlease123
If you want to try in live:
http://rubular.com/r/pFMkk9ATc0
Thank you
You may use
/^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$/
See the regex demo (a bit modified since the input is a multiline string).
Details:
^ - start of a string
(?=[^a-zA-Z]*[a-zA-Z]) - a positive lookahead requiring that there must be at least one ASCII letter after any 0+ chars other than ASCII letters
(?=[^0-9]*[0-9]) - a positive lookahead requiring that there must be at least one ASCII digit after any 0+ chars other than ASCII digits
[a-zA-Z0-9]* - 0+ ASCII letters or digits
$ - end of string.
I think this simplest one will also be helpful
Regex demo
Regex: ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z\d]+$
1. ^ start of string.
2. (?=.*\d) positive look ahead for digit.
3. (?=.*[a-zA-Z]) positive look alphabets.
4. [a-zA-Z\d]+ match all digits A-Z and a-z
5. $ end of string.
I am trying to validate a password with the following rules:
Must have at least eight characters.
Must contain ONLY letters and digits.
Must contain at least two digits.
So far I wrote this code:
[0-9a-zA-Z] (?=(.*\d){2}) {8,}
Im not sure why the passwords I enter returns invalid although it follows the rules.
Remember that spaces are meaningful in a regex pattern, so you require at least 8 spaces at the end. There are no anchors in the regex, so the length limitation might not work even if you write a correct pattern. So far, this will match an alphanumeric, a space that is followed with 2 occurrences of any 0+ chars followed with a digit, but since there is space with {8,} quantifier, this pattern will never match anything.
You need
^(?=.{8})[a-zA-Z]*(?:\d[a-zA-Z]*){2}[a-zA-Z0-9]*$
See the regex demo
^ - start of string
(?=.{8}) - at least 8 chars
[a-zA-Z]* - zero or more letters
(?:\d[a-zA-Z]*){2} - 2 sequences of:
\d - a digit (may be replaced with [0-9])
[a-zA-Z]* - zero or more letters
[a-zA-Z0-9]* - 0+ alphanumeric chars
$ - end of string.
Alternatively, you may use
^(?=(?:[a-zA-Z]*\d){2})[a-zA-Z0-9]{8,}$
See another regex demo
Here, (?=(?:[a-zA-Z]*\d){2}) will require at least 2 occurrences of 0+ letters followed with a digit in the string and [a-zA-Z0-9]{8,} will match 8 or more alphanumeric chars.