Javascript regex with below validation rules - javascript

Allowable characters:
uppercase A to Z
lowercase a to z
hyphen
apostrophe
single quote
space
full stop
numerals 0 to 9
validations:
Must contain alphabetic characters
Cannot have consecutive non alpha characters except for full stop followed by a space OR apostrophe can be followed by a space
Cannot have non-alphabetic characters at the start (except for apostrophe)
Can end with a full stop
I have a regex with the below validation. Use this as reference
/^(?=[a-zA-Z0-9`'. -]+$)(?!.*[0-9'` -]{2})[a-zA-Z'][^\r\n.]*(?:\.[ a-z][^\r\n.]*)*$/;
Need to add the below validations to the above regex
Can end with a full stop
An apostrophe can be followed by a space.
Examples Valid
'Andy
Andy.
Andy' De'Wall
Andy. DeWa2e
A2dy'
Examples Invalid
2Andy
A2'ndy.
Andy'-Wall
Andy. DeWa23
A24dy'

You could:
assert not 2 consecutive digits . ' or -
assert not a digit or hyphen or space followed by a space
assert at least a char A-Z a-z
start the match with either ' or a char A-Z a-z
For example
^(?!.*[0-9.'-]{2})(?!.*[0-9 -] )(?=[^A-Za-z\n]*[A-Za-z])[A-Za-z'][A-Za-z0-9.' -]*$
Regex demo

Related

Improve JS Regex pattern for name validation

I am trying to create a regex pattern for name validation.
Application name must have the following:
Lowercase alphanumeric characters can be specified
Name must start with an alphabetic character and can end with alphanumeric character
Hyphen '-' is allowed but not as the first or last character
e.g abc123, abc, abcd-1232
This is what I got [^\[a-z\]+(\[a-z0-9\-\])*\[a-z0-9\]$][1] it doesn't work perfectly. The validation fails if you enter a single character in the field. How can I improve this pattern? Thank you in advance.
You may use the following pattern:
^[a-z](?:[a-z0-9-]*[a-z0-9])?$
Explanation:
^[a-z] starts with lowercase alpha
(?: turn off capture group
[a-z0-9-]* zero or more alphanumeric OR dash
[a-z0-9] mandatory end in alphanumeric only, if length > 1
)? make this group optional
$ end of input
You are using a negated character class [^ that matches 1 character, not being any of the specified characters in the character class.
That is followed by another character class [1] which can only match 1, so the pattern matches 2 characters, like for example #1
In your examples you seem to have only a single hyphen, so if there can not be consecutive hyphens --
^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
[a-z0-9]* Optionally repeat any of a-z or 0-9
(?:-[a-z0-9]+)* Optionally repeat matching - followed by at least 1 or a-z or 0-9 (So there can not be a hyphen at the end)
$ End of string
Regex demo

Trouble with regular expression catching strings that begins and ends with alpha character and contains dash

I'm trying to create a regular expression that matches strings such as:
N1-112S
So far I have succeeded with the following (although I'm not really sure why it works):
item.match(/^\D.-/)
I'd like to further bolster the results by ensuring that the last character is A-Z as well.
I'd appreciate some help on a good regular expression for matching this pattern. Thanks!
If you plan to match a string that starts with an uppercase ASCII letter, then has a digit, then a hyphen, then 1 or more digits and then an ASCII letter at the end of the string use
/^[A-Z]\d-\d+[A-Z]$/.test(item)
See the regex demo. Also, to test if a regex matches some string or not, I'd recommend RegExp#test.
Pattern details
^ - start of string
[A-Z] - an uppercase ASCII letter
\d - an ASCII digit
- - a hyphen
\d+ - 1+ digits
[A-Z] - an ASCII letter
$ - end of string.
Variations
To match any alphanumeric chars after hyphen till the end of string, you need to change the above pattern a bit:
/^[A-Z]\d-[\dA-Z]*[A-Z]$/
The second \d+ is changed to [\dA-Z]*, any 0 or more ASCII digits or letters.
If there can be any chars after -, use .* or [^] instead of a \d+:
/^[A-Z]\d-.*[A-Z]$/

Issue with javascript regex not matching less than 3 characters

I have the following javascript regex:
/^[^\s][a-z0-9 ]+[^\s]$/i
I need to allow any alphanumeric character as well as spaces inside the string but not at the beginning nor at the end.
Oddly enough, the above regex will not accept less than 3 characters, e.g. aa will not match but aaa will.
I am not sure why. Can anyone please help ?
You have: [^\s] (requires matching at least one non-whitespace character), [a-z0-9 ]+ (requires matching at least one alphanumeric or space character), and [^\s] again (requires matching at least one non-whitespace character). So, in total, you need at least 3 characters in the string.
Use word boundaries at the beginning and end instead:
/^\b[a-z0-9 ]+\b$/i
https://regex101.com/r/2GhH3N/1
Try the following regex:
^(?! )[a-z0-9 ]*[a-z0-9]$
Details:
^(?! ) - Start of the string and no space after it (so here we exclude the
initial space).
[a-z0-9 ]* - A sequence of letters, digits and spaces, possibly empty
(the content before the last letter(see below).
[a-z0-9]$ - The last letter and the end of string (so here we exclude the
terminal space).
You should re-write the expression as
/^[a-z0-9]+(?:\s+[a-z0-9]+)*$/i
See the regex demo.
NOTE: If only one whitespace is allowed between the alphanumeric chars use
/^[a-z0-9]+(?:\s[a-z0-9]+)*$/i
^^
Details
^ - start of string
[a-z0-9]+ - 1+ letters/digits
(?:\s+[a-z0-9]+)* - 0 or more repetitions of 1+ whitespaces (\s+) and 1+ digit/letters
$ - end of string.
See the regex graph:

Regular expression to only allow letters and space in input filed but the first character should not be space

New to regex. I am validating a input filed. I need to write regular expression which checks the following:
Only letters and spaces are allowed not numbers and special charactes
The first character should be a valid letter [a-zA-Z] not a space.
Thank you
The following regex accepts only letters or spaces as you wanted.
^(?! )[A-Za-z\s]*$
Quick explanation:
^ Matches beginning of input
(?! ) Zero-width negative look-ahead assertion a.k.a. "not followed by...", so the first letter won't be a space character
[A-Za-z\s]* Accept only letters (A-Za-z) and spaces (\s) any time (*)
$ Matches end of input
Edit: If you don't want to match an empty string, then need to accept at least one character instead of any : ^(?! )[A-Za-z\s]+$

jQuery RegEx. If string contains any character except

I've got this RegEx which is used to validate what the user enters
It must be a value 8 - 16 characters long and can contain ONE of the certain special characters.
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~]).{8,16}$"
I'm not trying to show an alert if the user enters something that doesn't match the above. So a-z, A-Z, 0-9 and :;#~ are allowed but anything else shows an alert.
So Abcd1234# is OK, but if they enter Abcd1234!$ if will show the alert as ! & $ are not in the match.
I've tried adding ^ to the start of character match to try and negate them, but that didn't work.
What's the best way to do this?
It seems you only need to allow the characters mentioned in the lookaheads, create a character class with them and replace the last . with it:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[:;#~])[\da-zA-Z:;#~]{8,16}$/
^^^^^^^^^^^^^^
See the regex demo
The [\da-zA-Z:;#~]{8,16} pattern will match 8 to 16 chars that are either digits, ASCII letters, or :, ;, # or ~ symbols.
Details:
^ - start of string
(?=.*\d) - there must be a digit after any 0+ chars other than line break chars (a (?=\D*\d) will be more efficient as it is based on the contrast principle)
(?=.*[a-z]) - - there must be an ASCII lowercase letter after any 0+ chars other than line break chars (a (?=[^a-z]*[a-z]) will be more efficient)
(?=.*[A-Z]) - there must be an ASCII uppercase letter after any 0+ chars other than line break chars (a (?=[^A-Z]*[A-Z]) will be more efficient)
(?=.*[:;#~]) - there must be a :, ;, # or ~ after any 0+ chars other than line break chars (you may also use (?=[^:;#~]*[:;#~]))
[\da-zA-Z:;#~]{8,16} - 8 to 16 chars defined in the character class
$ - end of string.

Categories