Regular expressions: prohibit the use of characters [duplicate] - javascript

I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.

Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.

I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.

The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.

While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"

This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))

And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)

Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable

What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.

If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)

Related

Javascript Password Generator how to ensure password meets criteria [duplicate]

What is the regex to make sure that a given string contains at least one character from each of the following categories.
Lowercase character
Uppercase character
Digit
Symbol
I know the patterns for individual sets namely [a-z], [A-Z], \d and _|[^\w] (I got them correct, didn't I?).
But how do I combine them to make sure that the string contains all of these in any order?
If you need one single regex, try:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*\W)
A short explanation:
(?=.*[a-z]) // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z]) // use positive look ahead to see if at least one upper case letter exists
(?=.*\d) // use positive look ahead to see if at least one digit exists
(?=.*\W) // use positive look ahead to see if at least one non-word character exists
And I agree with SilentGhost, \W might be a bit broad. I'd replace it with a character set like this: [-+_!##$%^&*.,?] (feel free to add more of course!)
Bart Kiers, your regex has a couple issues. The best way to do that is this:
(.*[a-z].*) // For lower cases
(.*[A-Z].*) // For upper cases
(.*\d.*) // For digits
(.*\W.*) // For symbols (non-word characters)
In this way you are searching no matter if at the beginning, at the end or at the middle. In your have I have a lot of troubles with complex passwords.
Bart Kiers solution is good but it misses rejecting strings having spaces and accepting strings having underscore (_) as a symbol.
Improving on the Bart Kiers solution, here's the regex:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])((?=.*\W)|(?=.*_))^[^ ]+$
A short explanation:
(?=.*[a-z]) // use positive look ahead to see if at least one lower case letter exists
(?=.*[A-Z]) // use positive look ahead to see if at least one upper case letter exists
(?=.*\d) // use positive look ahead to see if at least one digit exists
(?=.*\W) // use positive look ahead to see if at least one non-word character exists
(?=.*_) // use positive look ahead to see if at least one underscore exists
| // The Logical OR operator
^[^ ]+$ // Reject the strings having spaces in them.
Side note: You can try test cases on a regex expression here.
You can match those three groups separately, and make sure that they all present. Also, [^\w] seems a bit too broad, but if that's what you want you might want to replace it with \W.

JavaScript Regex to match 2 words and an whitespace character with length limitations

I actually got this regex ^[A-zÀ-ÿ ]{3,50}$ or ^[A-zÀ-ÿ\s]{3,50}$ that finds 3 to 50 characters of this specific alphabets.
I need a new regex to accept only 1 whitespace character \s maintaining the {3,50} limitation.
I tried ^[A-zÀ-ÿ]+\s[A-zÀ-ÿ]{3,50}$ but it is limiting the last tuple to 3-50 and not the whole thing.
Any help would be appreciated,
Thank you
Actually, to match ASCII letters, you need to use [A-Za-z], not [A-z] (see this SO thread).
As for the single obligatory whitespace, it can be added as in your attempt, and the length limitation can be added in the form of a lookahead:
/^(?=.{3,50}$)[A-Za-zÀ-ÿ]+\s[A-Za-zÀ-ÿ]+$/
^^^^^^^^^^^^
See the regex demo.

Regex JavaScript - how to check if a string is all letters and then all numbers

This would be ok: 'AAAAAAA1222222'
This would be not ok: '1AAAAA'
This would not be ok: 'AA1AA'
Just looking for a way to check if a string is ALL letters and then ONLY letters afterward.
This is an easy one.
^[A-Za-z]*[0-9]*$
That of course is assuming that no letters is OK.
For example, the above example would match
AAAAAAA
2222222
as well as an empty string.
If there must be at least one letter and at least one number, replace the * with +
^[A-Za-z]+[0-9]+$
text.match(/^[A-Z]*\d*$/i)
Read this as "start of string followed by any number of letters followed by any number of digits followed by the end of the string."
Note this will match "", "A", and "1". If you want there to be at least one letter and at least one number, use + instead of * in both spots.
Use a lookahead. Lookaheads are used for validation, I suggest you go through this.
Try this out: ^(?=[A-Za-z]*\d*$).+
DEMO
try this regex
\b\D+\d+\b
\b is the word boundary that won't allow for digits to come in the beginning and letters to come after the digits at the end.
I suggest something like this:
text.match(/\b\D+\d+[^\D]\b/);
The \b was suggested by Grace and is a good idea. Another option is to use the beginning and end anchors like:
text.match(/^\D+\d+[^\D]$/);
text.match(/^[a-zA-Z]+\d*$/);
Tests:
AAAAAAA1222222 - match
1AAAAA - no match
AA1AA - no match
AAAAAAA - match
2222222 - no match
If you dont want to match ALL Letters and at least one number change the quantifier of \d from +(1-infinite times) to *(0-infinity times)
more about regex quantifiers : link

Regex lookaround for a group doesn't work

Happy Saturday,
I'm wondering if Stackoverflow's users could give me a clue about one specific Regex..
(^visite\d+)(?!\D)
The above regex works well..
It says that :
visite12345 --> is a good anwser (the string does match)
visite1a --> is not a good anwser (the string doesn't match)
However for:
visite12345a --> It doesn't work.
Indeed, the output is visite1234, whereas I'd like to get the same answer that for visite1a (string doesn't match)...
I use http://regexr.com/ to test my regexp.
Do you have any idea how to so?
Thank you very much.
The regex (^visite\d+)(?!\D) matches visite at the start of the string, followed with one or more digits that should not be followed with a non-digit.
The "issue" is that the engine can backtrack within \d+ pattern and it can match 2 digits if the third is not followed with a nondigit.
The best way to solve it is to check the actual requirements and adjust the pattern.
If the digits are the last characters in the string you just should replace the lookahead with the $ anchor.
A generic solution for this is making the subpattern atomic with a capturing group inside a positive lookahead and a backreference, and make sure the lookahead is changed to something like (?![a-zA-Z]) - fail if there is a letter):
/^visite(?=(\d+))\1(?![a-z])/i
See the regex demo
Or if a word boundary should follow the digits (i.e. digits should be followed with a letter, digit or an underscore), use \b instead of the lookahead:
/^visite\d+\b/
See another demo

(js)regular expression for matching a words only

I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/

Categories

Resources