This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I'm going to check the username via both JavaScript then PHP.
The username can contain English letters, numbers and one hyphen (-).
The username cannot be started with hyphen (-).
The username cannot be finished with hyphen (-).
The username cannot be started with numbers.
The username cannot contain more than one hyphen (-).
The username cannot be shorter than 6 and longer than 20.
abc123 is correct.
abc-123 is correct.
ab12 is wrong: username is shorter than 6 character.
-abc123 is wrong: username is started with hyphen.
abc123- is wrong: username is finished with hyphen.
ab-12-c3 is wrong: username contains more than one hyphen.
123abc is wrong: username is started with numbers.
You can use
^(?!\d)(?!.*-.*-)(?!.*-$)(?!-)[a-zA-Z0-9-]{6,20}$
See demo
Explanation:
^ - Match at the beginning
(?!\d) - Do not match if the string starts with a digit
(?!.*-.*-) - Do not match if the string has 2 hyphens in the string
(?!.*-$) - Do not match if the string ends with a hyphen
(?!-) - Do not match if the string starts with a hyphen
[a-zA-Z0-9-]{6,20} - Match 6 to 20 characters from the range specified
$ - Assert the end of string.
You need to use a lookahead based regular expression:
/^(?=.{6,20}$)[a-z][a-z0-9]+(?:-[a-z0-9]+)?$/i
A logical approach using JavaScript, would be:
function _isvalid(str) {
var re = /^[a-z][a-z0-9]+(?:-[a-z0-9]+)?$/i
return ((str.length > 5) && (str.length < 21) && re.test(str))
}
Related
This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
Closed 1 year ago.
Need regex for 6 or 10 digit number can be start and end with space, can also be a single word in a whole string. It should not start and end with '/'.
For example:
My Zip-code is 101010. Please update. -> 101010
9090909090 is my mobile number -> 9090909090
00/9090909090/000000/ -> should not find any number
9090909090 -> 9090909090
I have tried this
\b(\d{11}|\d{10}|\d{6})\b
regex, but unable to handle 3rd situation.
You can use
\b\d{6}(?:\d{4})?\b(?!\/)
See the regex demo and the regex graph:
Details:
\b - a word boundary
\d{6} - any six digits
(?:\d{4})? - an optional sequence of four digits
\b - a word boundary
(?!\/) - there must be no / immediately to the right of the current location (this is a negative lookahead).
Question
Here are some simple rules that users have to follow when creating their username.
1) Usernames can only use alpha-numeric characters.
2) The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.
3) Username letters can be lowercase and uppercase.
4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
My Code
let username = "JackOfAllTrades";
let userCheck = /^(a-za-z|a-z(a-z+|\d\d+))(\d*)$/i;
let result = userCheck.test(username);
My Question
How can I fix this code?
What is it about the code that doesn't work?
Solution 1:
let username = "JackOfAllTrades";
let userCheck = /^[a-z]([0-9][0-9]+|[a-z]+\d*)$/i;
let result = userCheck.test(username);
Code Explanation
^ - start of input
[a-z] - first character is a letter
[0-9][0-9]+ - ends with two or more numbers
| - or
[a-z]+ - has one or more letters next
\d* - and ends with zero or more numbers
$ - end of input
i - ignore case of input
Solution 2:
let username = "JackOfAllTrades";
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username);
Code Explanation
^ - start of input
[a-z] - first character is a letter
[0-9]{2,0} - ends with two or more numbers
| - or
[a-z]+ - has one or more letters next
\d* - and ends with zero or more numbers
$ - end of input
i - ignore case of input
Here is my solution:
/^[a-z][a-z]+$|^[a-z]+\w\d+$/i
/^ - at the beginning of the string, find
[a-z][a-z]+$ - at least 2 letters, could be more, till the end(this means that strings like test12 will not pass in this one)
| - OR (to the cases where it has more numbers and more than 2 characters
^[a-z]+ - beginning with any letter, can be more
\w - any character, can be a-z and 0-9, it is used this way to force the string to have at least 3 characters
\d+$ - ending with a chain with at least one number(if it doesn't have at least one number at the end, it will match in the first of the conditional)
/i - consider caps characters
Edit: I missed something in the OP requirements: there can be only one leading letter if the username is more than 2 characters long. So I corrected this answer accordingly, and we fundamentally get the same regex as Venkatesh's solution 2.
I supposed that you wish only non-accented characters.
With the regular expression /^[a-z]([a-z]+\d*|\d{2,})$/i (test it here), you get the following matches/failures (when testing one by one):
• Paul46: matches
• 4frank: fails
• mike: matches
• jus6tin: fails
• p87: matches
• k9: fails
• AL10: matches
Try this one:
let userCheck = /^[a-z]([a-z]+|[a-z]*[\d][\d])$/i;
let userCheck = /^[a-z]([a-z]+|[0-9]\d+)\d*$/i;
Above passes all test cases of this problem
The below solution works fine to find a username ased on the below conditions
Usernames can only use alpha-numeric characters.
The only numbers in the username have to be at the end. There can be zero or 2. more of them at the end. Username cannot start with the number.
Username letters can be lowercase and uppercase.
Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.
let username = "JackOfAllTrades";
let userCheck = /^[a-z]+(\d\d+$|[a-z]+\d*$)/i; // Change this line
let result = userCheck.test(username);
console.log(result)
Description:
^[a-z]+ - to match one or more(+) alphabet([a-z]) in the beginning(^).
\d\d+$ - to match the ending 2 or more(\d for one and \d+ for one or more) number if only one alphabet in the beginning.
[a-z]+\d*$ - to match one or more alphabet along with 0 or more number at the end.
i - flag for ignoring letter case
| - sign to pick match both regex
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Hi i have a field that allows only alphabet or numeric in First character, and the rest only allow numeric only, and no allow symbols/space etc.
how to make the jquery/code?
Very simple regex:
/^[a-z0-9]\d*$/
Elaborating Jack answer little bit and convert to working snippet.
$('#yourid').on("keyup", function() {
let val = $(this).val();
let reg = /^[a-z0-9]\d*$/
let newval = val.replace(reg, '');
if (!val.match(reg)) {
$(this).val(''); // clear the field if pattern don't match.
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' id='yourid' />
How above regex work.
^ asserts position at start of the string
Match a single character present in the list below [a-z0-9]
a-z a single character in the range between a and z (case sensitive)
0-9 a single character in the range between 0 and 9 (case sensitive)
\d* matches a digit (equal to [0-9])* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed
$ asserts position at the end of the string, or before the line
terminator right at the end of the string (if any)
This question already has answers here:
Regular expression to match at least two special characters in any order
(3 answers)
javascript regex for special characters
(15 answers)
Closed 3 years ago.
I am not very confortable with regex. I would like the password to have a minimum of 6 characters, including 2 special characters
I tried a first regex but it just returns false each time...
console.log(/^[a-zA-Z0-9!$#%]+$/.test(this.form.password));
I can test the number of characters another way but I would like to include the two special characters through the regex.
Thanks a lot in advance for your help
First lookahead for 6 characters, to ensure that enough exist in the input. Then, repeat twice: 0 or more normal characters, followed by a special character, to ensure that there are at least 2 special characters in the input. Then match 0 or more [special or normal] characters.
/^(?=.{6})(?:[a-z\d]*[!$#%]){2}[a-z\d!$#%]*$/i
https://regex101.com/r/ZyFZPm/2
^ - Start of string
(?=.{6}) - At least 6 characters
(?:[a-z\d]*[!$#%]){2} - Repeat twice:
[a-z\d]* - 0+ alphanumeric characters
[!$#%] - 1 special character
[a-z\d!$#%]* - Match both special and alphanumeric characters up to the end of the string
$ - End of string
Hi i have a field in php that will be validated in javascript using i.e for emails
var emailRegex = /^[\w-\.]+#([\w-]+\.)+[\w-]{2,4}$/;
What i'm after is a validation check which will look for the
first letter as a capital Q
then the next letters can be numbers only
then followed by a .
then two numbers only
and then an optional letter
i.e Q100.11 or Q100.11a
I must admit i look at the above email validation check and i have no clue how it works but it does ;)
many thanks for any help on this
Steve
The ^ marks the beginning of the string, $ matches the end of the string. In other words, the whole string should exactly match this regular expression.
[\w-\.]+: I think you wanted to match letters, digits, dots and - only. In that case, the - should be escaped (\-): [\w\-\.]+. The plus-sign makes is match one or more times.
#: a literal # match
([\w-]+\.)+ letters, digits and - are allowed one or more times, with a dot after it (between the parentheses). This may occur several times (at least once).
[\w-]{2,4}: this should match the TLD, like com, net or org. Because a TLD can only contain letters, it should be replaced by [a-z]{2,4}. This means: lowercase letters may occur two till four times. Note that the TLD can be longer than 4 characters.
An regular expression which should follow the next rules:
a capital Q (Q)
followed by one or more occurrences of digits (\d+)
a literal dot (.)
two digits (\d{2})
one optional letter ([a-z]?)
Result:
var regex = /Q\d+\.\d{2}[a-z]?/;
If you need to match strings case-insensitive, add the i (case-insensitive) modifier:
var regex = /Q\d+\.\d{2}[a-z]?/i;
Validating a string using a regexp can be done in several ways, one of them:
if (regex.test(str)) {
// success
} else {
// no match
}
var emailRegex = /^Q\d+\.\d{2}[a-zA-Z]?#([\w-]+\.)+[a-zA-Z]+$/;
var str = "Q100.11#test.com";
alert(emailRegex.test(str));
var regex = /^Q[0-9]+\.[0-9]{2}[a-z]?$/;
+ means one or more
the period must be escaped - \.
[0-9]{2} means 2 digits, same as \d{2}
[a-z]? means 0 or 1 letter
You can check your regex at http://regexpal.com/