How to write Regular expression for minimum one character in javascript? - javascript

I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.
/^[a-zA-Z0-9\-\_\/\s,.]+$/
and
/^([a-zA-Z0-9]+)$/

I need minimum of one letter of Alphabets
[a-z]+
and followed by numbers and special characters.
[0-9_\/\s,.-]+
Combined together you would get this:
/^[a-z]+[0-9_\/\s,.-]+$/i
The /i modifier is added for case insensitive matching of alphabetical characters.

Try this regex:
/^[a-z][\d_\s,.]+$/i
To clarify what this does:
^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive

You need the other character selection to be separate. I'm confused as to what "numbers and special characters" means, but try:
/^[a-z]+[^a-z]+$/i

Related

Number or special character requirement addition to password validation regex

I need to validate password with: At least one uppercase, at least one lowercase, at least one number OR symbol, at least 8 characters.
I have this regex:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,20}$/
This works fine except of > it checks string on number AND symbol, but not on number OR symbol. And also character length 8-20, not at least 8 but gives range. I want it to check number OR symbol. Any ideas? Thanks and have a good day!
The (?=.*\d) positive lookahead requires a digit in the string AND (?=.*[^a-zA-Z0-9]) requires a char other than ASCII letter or digit.
To make the regex require a digit OR a char other than ASCII letter or digit, merge the two lookaheads as
(?=.*[^A-Za-z])
Basically, you need to remove 0-9 from the second lookahead and it will require any char but an ASCII letter.
Result:
/^(?=.*[^A-Za-z])(?=.*[a-z])(?=.*[A-Z]).{8,20}$/
Or, a much more efficient version based on the contrast principle:
/^(?=[A-Za-z]*[^A-Za-z])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
See the regex demo.
If a space is not special, add it to the lookahead:
/^(?=[A-Za-z ]*[^A-Za-z ])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
^ ^

JavaScript Regular Expression OR Operator

Saw a challenge on Twitter so I've been working my way through it, granted I am not the best with Regular Expressions. This is what I have so far:
var pass_regex = new RegExp(/^[a-z][A-Z][0-9]|[!##$%^&*()_]+$/);
I am trying to match a password input that contains:
1 Lowercase Letter
1 Uppercase Letter
1 Digit OR Special Character
Where I am getting stuck is on the 'OR' part, I thought the pipe separator between [0-9] and my set of special characters would work but it doesn't seem to. Trying to better understand how you would use regular expressions to to check for 1 Digit OR 1 Special Character. Thank you in advance for any help provided.
Atleast one:
You need to use a positive lookahead based regex for checking multiple conditions.
^(?=.*?[A-Z])(?=.*?[a-z]).*?[\W\d].*
OR
^(?=.*?[A-Z])(?=.*?[a-z]).*?[!##$%^&*()_\d].*
(?=.*?[A-Z]) Asserts that there must be atleast one uppercase letter.
(?=.*?[a-z]) Atleast one lowercase letter.
.*? non-greedy match of any character zero or more times.
If the above conditions are satisfied then match that corresponding string and also the string must contain atleast a single character from the given list [!##$%^&*()_\d] . \d in this list matches any digit character.
.* matches the following zero or more characters.
DEMO

C# Regex for three digit and an alpha

I have a field where I need to have a regex where the first 3 digits are numeric and the fourth character should be alpha- letter only, I need to have regex in both c# and javascript.
My following regex is good for three numeric number
#"\A(\d){3}\Z";
How to add for the fourth character which has to be alpha
If by alpha you mean only latin letters, you can do this:
^\d{3}[a-zA-Z]$
You can't use \A and \Z in JavaScript but they're equivalent to ^ and $ unless you use the m option.
If you need full Unicode character range, use \p{L} instead of [a-zA-Z], but you're out of luck for JavaScript support. You'd have to include the relevant Unicode ranges by hand into the character class...
I think what you want to know about is Character Classes or Character Sets
With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. [...]
and
[...] You can use a hyphen inside a character class to specify a range of characters. [0-9] matches a single digit between 0 and 9. You can use more than one range. [0-9a-fA-F] matches a single hexadecimal digit, case insensitively. You can combine ranges and single characters. [0-9a-fxA-FX] matches a hexadecimal digit or the letter X. Again, the order of the characters and the ranges does not matter. [...]
So basically you can match character "A" to "Z" by placing them in square brackets and using a hyphen to indicate range
[A-Z]
You can match multiple sets, so if you also need "a" to "z" (lowercase) you can include
[A-Za-z]

Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number)

I'm currently using this regex (/^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/) to accept letters, numbers, spaces and underscores. I want to change it like this that it takes the combination of number and the character but not only the number.
If i understand correctly you want to allow a string that begins with at least one letter and optionally is followed by number or underscore or space.
Try this: /^(?:[A-Za-z]+)(?:[A-Za-z0-9 _]*)$/ at this online regex tester.
This should work.
Cheers!
Try this:
/^[A-Za-z0-9 _]*[A-Za-z]+[A-Za-z0-9 _]*$/
This allows for 0 or more of each of the outside groups, and 1 or more of the inner group (letters only). A string of only digits will fail.
/^[\w\s]+$/
\w allows letters, numbers and underscores
\s allow spaces
Try this:
^(?![0-9]*$)[a-zA-Z0-9\s_]+$
This expression has a negative lookahead to verify that the string is not only numbers. See it in action with regexr

need a regex for password validation that allows all special characters [duplicate]

This question already has an answer here:
Password validation (regex?)
(1 answer)
Closed 8 years ago.
The password requirements are:
at least two letters
at least two numbers
at least one special character (any special character)
at least 8 characters
This one is close but isn't working:
/^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W]).{8,}$/
What am I doing wrong?
This regex meets your requirements:
/^(?=(?:[^a-z]*[a-z]){2})(?=(?:[^0-9]*[0-9]){2})(?=.*[!-\/:-#\[-`{-~]).{8,}$/i
Play with the demo to see what matches and doesn't match.
Explanation
This is a classic password validation technique with lookarounds as explained in this article
The i flag at the end makes it case-insensitive so we don't have to say a-zA-Z
The ^ anchor asserts that we are at the beginning of the string
The first lookahead (?=(?:[^a-z]*[a-z]){2}) asserts that what follows at this position (the beginning of the string) is any characters that are not a letter, followed by one letter... twice, ensuring there are at least two letters
The second lookahead (?=(?:[^0-9]*[0-9]){2}) asserts that what follows at this position (still the beginning of the string) is any characters that are not a digit, followed by one digit... twice, ensuring there are at least two letters
The third lookahead (?=.*[!-\/:-#\[-{-~])` asserts that what follows at this position (still the beginning of the string) is any characters, followed by one special character
The $ anchor asserts that we are at the end of the string
Note about special characters
The regex [!-\/:-#\[-{-~]` specifically picks out all printable chars that are neither digits nor letters from the ASCII table. If this includes chars you don't want, make it more restrictive.
A regex is probably inappropriate for this; it's hard to glance at the regex you've got and immediately have any idea what the requirements are, let alone how to modify them. You might want to just count the number of characters in each group directly, then check that those counts all pass the appropriate threshold.
That said: consider that this would enforce really awkward passwords, yet disallow xkcd-style passwords. I strongly encourage you to take a more heuristic approach, where a longer password loosens the other restrictions. There are other considerations to enforcing a strong password, too, like similarity to dictionary words and number of unique characters.
Honestly you might be best off just requiring passphrases :)
I'd say:
/^(?=.*\d.*\d)(?=.*[a-zA-Z].*[a-zA-Z])(?=.*[\W]).{8,}$/
Your regex was missing the 2 digits and 2 letters requirements.
How about:
/^(?=.{2,}\d)(?=.{2,}[a-zA-Z])(?=.*[\W]).{8,}$/
It should meet your requirement.
Depends on what you consider to be a "special character". If a special character is anything that is not a digit or a letter, and if Spaces are not allowed in the password, then:
^(?=(?:\S*\d){2})(?=(?:\S*[A-Za-z]){2})(?=\S*[^A-Za-z0-9])\S{8,}
or, with the "escapes":
"^(?=(?:\\S*\\d){2})(?=(?:\\S*[A-Za-z]){2})(?=\\S*[^A-Za-z0-9])\\S{8,}"
If you choose to allow spaces, replace \S with a dot .
If you want to define "special characters" as only including certain characters, or as excluding other characters in addition to letters and digits, edit the character class in the final lookahead.

Categories