Validating userName using Regex - javascript

The only numbers in the username have to be at the end. There can be zero or more of them at the end.
Username letters can be lowercase and uppercase.
Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.
I'm trying with this but I'm stalled. /\d+$\w+/gi

/^[a-z]{2,}\d*$/i is:
^ : the begining
[a-z] : a character (a to z), you can add as many allowed characters as you want
{2,} : at least 2 of them
\d* : 0 or more digits
$ : the end
i : ignore case sensetivity (both lowercases and uppercases are allowed)

Username having characters and digit and min 2 character long
/^[a-zA-Z]{2,}\d*$/i
Test result :
UserNam9 = pass
9username = fail
Userna99 = pass
usernameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = pass
Us = pass
U = fail

/^[A-z]{2,}[A-z0-9]{0,}$/
/^ // start of line
[A-z]{2,} //alphabet characters 2 or more
[A-z0-9]{0,} //numbers and alphabet
$/ // end of line

You've missed cases when there's a letter in the start, followed by 2 or more numbers.
U99 = fail
d345 = fail
My solution passes these tests, as well:
/^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i
Using positive lookahead I am making sure that in the second case there are at least 3 alphanumeric characters.

Simplified version of /^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i:
/^\D(\d{2,}|\D+)\d*$/i
Code explanation:
^ - start of input
\D - first character is a letter
\d{2,} - ends with two or more numbers
| - or
\D+ - has one or more letters next
\d* - and ends with zero or more numbers
$ - end of input
i - ignore case of input

This is my answer, it passed all the tests:
/^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i
First Part: 2 letters (or more) and zero or more numbers
Or
Second Part: 1 letter and 2 or more numbers

Related

Regex Js must have 1 alphabet and 1 character

the following is my regex
regex: /^[a-zA-Z]+-.*(?=.*\d)(?=.*[a-zA-Z]).*-[0-9]+-[a-zA-Z]+-[0-9]+$/
the overall rules is: alphabet-alphanumeric-number-alphabet-number
im having problem at .*(?=.*\d)(?=.*[a-zA-Z]).*
the expected output for it to get a successful result for the alphanumeric part is
abc123
123abc
1abc23
ab23ca
and fail if
abcde
12345
but the result i get is all successful include the expected fail result
abc-abc-123-abc-123
abc-123-123-abc-123
i see that using lookahead will also get the input after the dash(-) of the alphanumeric that caused it to be successful although it is not the expected result
You could try
^[a-zA-Z]+-(?![a-zA-Z]+-|\d+-)[a-zA-Z0-9]+-\d+-[a-zA-Z]+-\d+$
It uses this negative lookahead to check the second subsequence is not only made by alphabets or numerics
(?![a-zA-Z]+-|\d+-)
Alternatively, you can use this positive lookahead to check the second subsequence is made by a digit preceded any alphabets or the other way around
(?=[a-zA-Z]+\d|\d+[a-zA-Z])
It is important to use a lookahead to check it right at the start of the subsequence, and do not use .* in this situation since it might consume a - and checks the wrong sequence behind it.
You may check the test result here
For the alphanumeric part, you can assert not only digits till the next hyphen.
Using a case insensitive match:
^[a-z]+-(?!\d+-)[a-z]*\d[a-z\d]*-\d+-[a-z]+-\d+$
^ Start of string
[a-z]+- Match 1+ chars a-z and -
(?!\d+-) Negative lookahead, assert not only digits followed by -
[a-z]*\d[a-z\d]*- Match optional chars a-z, match a digit and optional chars a-z or a digit
\d+-[a-z]+-\d+ Match digits - chars a-z - digits
$ End of string
Regex demo
const regex = /^[a-z]+-(?!\d+-)[a-z]*\d[a-z\d]*-\d+-[a-z]+-\d+$/i;
[
"abc-abc-123-abc-123",
"abc-123-123-abc-123",
"abc-abcde-123-abc-123",
"abc-12345-123-abc-123",
"abc-ab23ca-123-abc-123",
"abc-1abc23-123-abc-123"
].forEach(s =>
console.log(`${s} --> ${regex.test(s)}`)
);
why not:
/([A-Za-z]+[0-9]+|[0-9]+[A-Za-z]+)/
-- or --
/[A-Za-z]/.test(val) && /[0-9]/.test(val)
var cases = [
"abc123",
"123abc",
"1abc23",
"ab23ca",
// fail
"abcde",
"12345",
"abc-abc-123-abc-123", // not clear if these should fail?
"abc-123-123-abc-123"
];
cases.forEach(val => {
var ok = /([A-Za-z]+[0-9]+|[0-9]+[A-Za-z]+)/.test(val)
//var ok = /[A-Za-z]/.test(val) && /[0-9]/.test(val);
console.log(val, ok);
});

Writing regular expression that will filter entered username

I want to write a regexp that will filter any username which:
Starts with number or letter (not case sensitive)
Can include - but cannot contain more than one in a row
example u-s-e-r✔ us-er✔ us--er✖
Also username cannot start with - or end with -
example -user✖ user-✖
It also needs to be at least 1 character (letter or number) and max 39.
The closest I've come to my result is something like this:
^[a-zA-Z\d](?:[a-zA-Z\d]|-(?=[a-zA-Z\d])){0,38}
This matches exactly what it should match, but it also matches some things that it shouldn't.
Basically these shouldn't be valid:
-username
_username_
__us_ername
us_er
username-
1user--name
132uname-
-uname1234
-username-
user--name
av34axc-
1234567890A1234567890B1234567890C1234567890D
And these should be valid:
Username
a-a
aBc
BaC
1-1
1-2-3-4
q-1-2-3
q-q-q-q-q
username
123username123
username3123
1234
user-name
13-13
q1-q2-q3
a
A
1234567890A1234567890B1234567890C123456
1234567890A123456-7890B1234567890C12345
You may use
^(?=.{1,39}$)[a-zA-Z\d]+(?:-[a-zA-Z\d]+)*$
See the regex demo and the Regulex graph:
Details
^ - start of string
(?=.{1,39}$) - the length must be 1 to 39 chars
[a-zA-Z\d]+ - 1+ alphanumeric chars
(?:-[a-zA-Z\d]+)* - 0 or more repetitions of
- - a hyphen
[a-zA-Z\d]+ - 1+ alphanumeric chars
$ - end of string.
You could use negative look ahead to implement the hyphen restrictions:
^(?!.*-(-|$)|-)[a-z\d-]{1,39}$

Issues in password regular expression

Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance
You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.

Regular Expression only digits and blank spaces

I have a text input and I require that it only accepts digits (0-9) and blank spaces (" ").
The current regexp to validate this input is:
/((\d{1,})(\s{1,})?){1,}/
Which stands for: one or more groups base on a first group of one or more digits and an optional second group of one or more blank spaces
That will only let me introduce values as: 999999 (only digits) or " " (only blank spaces) or 91 08 8510 903 (mix of digits and spaces).
But actually, I also can insert aaa or other characters.
Dissection
Your regular expression doesn't accept only letters :
/((\d{1,})(\s{1,})?){1,}/.test('aaa') // false
Actually, any character is accepted if the input contains at least one digit :
/((\d{1,})(\s{1,})?){1,}/.test('a1a') // true
That being said, let's skim the fat from your pattern :
"{1,}" equals "+" -> ((\d+)(\s+)?)+
"(.+)?" equals ".*" -> ((\d+)\s*)+
useless brackets -> (\d+\s*)+
This result can be translated to : "one or more digits (\d+) followed by zero or more blank spaces (\s*), one or more times (()+), anywhere in the input". Alternatively, we could say : "at least one digit, anywhere in the input".
What you need is to replace "anywhere in the input" with "from the beginning to the end of the input". This is allowed by the following special characters : ^ (beginning of input) and $ (end of input). Let's make a bunch of tests to see how they work :
requirement regex input .test()
---------------------------------------------------------------------
must contain at least one digit /\d+/ 'a1a' true
must start with at least one digit /^\d+/ '1a' true
must start with at least one digit /^\d+/ 'a1' false
must end with at least one digit /\d+$/ '1a' false
must end with at least one digit /\d+$/ 'a1' true
only digits from the beginning to the end /^\d+$/ '1a1' false
Suggestion
Only digits potentially separated by one whitespace : /^\d+( \d+)*$/.
^ beginning of the input
\d+ a digit, one or more times
( \d+)* a whitespace + same as above, zero or more times
$ end of the input
Usage example :
var r = /^\d+( \d+)*$/;
var isValid = r.test(' 1 '); // false
var isValid = r.test('1 1'); // true
var isValid = r.test('1 1'); // false
More about regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml.
try this one
$pattern = '/^[0-9 ]+$/';
if ( preg_match ($pattern, $text) )
{
echo 'allowed';
}
Try this regular expression
/(\d+\s*\d*)+$/
Visualize the results here http://regex101.com/r/dE5uR8
I have tested it online using
http://regexpal.com/
The above regular expression will not accept empty blank spaces at the start. You need atleast one digit. If you want to match the empty spaces at the start also change it to (\d*\s*\d*)+$ which will accept empty spaces also

Regex split string of numbers at finding of Alpha Characters

OK Regex is one of the most confusing things to me. I'm trying to do this in Javascript. I have a search field that the user will enter a series of characters. Codes are either:
999MC111
or just
999MC
There is ALWAYS 2 Alpha characters. BUT there may be 1-4 characters at the front and sometimes 1-4 characters at the end.
If the code ENDS with the Alpha characters, then I run a certain ajax script. If there are Numbers + 2 letters + numbers....it runs a different ajax script.
My struggle is I know \d is for 2 digits....but it may not always be 2 digits.
So what would my regex code be to split this into an array. or something.
I think correct regex would be (/^([0-9]+)([a-zA-z]+)([0-9]+)$/
But how do i make sure its ONLY 2 alpha characters in middle?
Thanks
You could use the regex /\d$/ to determine if it ends with a decimal.
\d matches a decimal character, and $ matches the end of the string. The / characters enclose the expression.
Try running this in your javascript console, line by line.
var values = ['999MC111', '999MC', '999XYZ111']; // some test values
// does it end in digits?
!!values[0].match(/\d$/); // evaluates to true
!!values[1].match(/\d$/); // evaluates to false
To specify the exact number of tokens you must use brackets {}, so if you know that there are 2 alphabetic tokens you put {2}, if you know that there could be 0-4 digits you put {0,4}
^([0-9]{0,4})([a-zA-z]{2})([0-9]{0,4})$
The above RegEx evaluates as follows:
999MC ---> TRUE
999MC111 --> TRUE
999MAC111 ---> FALSE
MC ---> TRUE
The splitting of the expression into capturing groups is done by means of grouping subexpressions into parentheses
As you can see in the following link:
http://regexr.com?2vfhv
you obtain this:
3 capturing groups:
group 1: ([0-9]{0,4})
group 2: ([a-zA-z]{2})
group 3: ([0-9]{0,4})
The regex /^\d{1,4}[a-zA-Z]{2}\d{0,4}$/ matches a series of 1-4 digits, followed by a series of 2 alpha characters, followed by another series of 0-4 digits.
This regex: /^\d{1,4}[a-zA-Z]{2}$/ matches a series of 1-4 digits, followed only by 2 alpha characters.
Ok so I didnt really care about the middle 2 characters....all that really mattered was the 1st set of numbers and last set of numbers (if any).
So essentially I just needed to deal with digits. So I did this:
var lead = '123mc444'; //For example purposes
var regex = /(\d+)/g;
var result = (lead.match(regex));
var memID = result[0]; //First set of numbers is member id
if(result[1] != undefined) {
var leadID = result[1];
}

Categories

Resources