Angularjs regex validation - javascript

I want to make an Angularjs regex validation that allow all alphabets, numbers, space and following special characters ! ' " & - ( )

You may use
ng-pattern="/^[a-zA-Z0-9\s!'\x22&()-]*$/"
Details
^ - start of string
[a-zA-Z0-9\s!'\x22&()-]* - 0 or more:
a-zA-Z - ASCII letters
0-9 - ASCII digits
\s - whitespaces
!'\x22&()- - !, ', " (\x22 matches "), &, (, ) or - (the hyphen must be at the end/start of the character class or escaped to denote a literal -)
$ - end of string.

Related

How to don't allow set only the same characters and only special sign - regex js

I'm looking for some regex pattern to:
not allow set the same characters
not allow only special signs.
So:
11111111 - not allow
1111811111 - allow
rrrrrrrr - not allow
rrrrrrrrrr5 - allow
11111111% - allow
%$!#$!#### - not allow
%%%%%%%%%1 - allow
I found regex which is helpful for recognize that string has only special sign: /^\W*\w.*$/, but I don't know how to join it with condition about not only repeated characters. Thansks for any advices.
You can assert not only the same characters and make sure to match at least a single word character.
^(?!(.)\1+$)[^\w\n]*\w.*$
Explanation
^ Start of string
(?!(.)\1+$) Negative lookahead, assert not only the same characters in the string matching at least 2 characters
[^\w\n]* Optionally match any character except a word character or newline
\w Match at least a single word character
.* Optionally match any character
$ End of string
Regex demo
const regex = /^(?!(.)\1+$)[^\w\n]*\w.*$/;
[
"11111111",
"1111811111",
"rrrrrrrr",
"rrrrrrrrrr5",
"11111111%",
"%$!#$!####",
"%%%%%%%%%1",
"a"
].forEach(s => {
if (regex.test(s)) {
console.log(s + " allow")
} else {
console.log(s + " not allow")
}
});
If you don't want to match any spaces in the string, you can match non whitespace chars using \S
^(?!(\S)\1+$)[^\w\s]*\w\S*$
Regex demo

Regex for validating statement descriptor for credit card statements

I'm trying to validate a string entered by the user to be used as the statement description on the credit card statement to describe the purchase.
The requirements are:
Must be between 5 and 22 characters long
Must contain at least one letter (case doesn't matter)
Cannot contain these characters: < > \ ' "
Only ASCII characters allowed
Here's what I've got so far, which is kind of working:
/^(?=.*?[a-zA-Z])[a-zA-Z0-9]{5,22}$/gm
...in that it correctly checks the length for 5-22 characters long and checks for at least one letter. However, it disallows all special characters and diacritics instead of just the few that aren't allowed. How do I modify it to allow the other allowed characters?
You could use a positive lookahead to assert a character and a negative lookahead to assert not to match any character listed in the character class.
For Javascript you can use the case insensitive flag /i and use [a-z].
Edit: As Wiktor Stribiżew points out, to match only ASCII characters you could use [\x00-\x7F] instead of using a dot.
^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$
^ Start of string
(?=.*[a-z]) Positive lookahead to check if there is a ASCII letter
(?!.*[<>\\'"]) Negative lookahead to check that there is not any of the chars in the character class
[\x00-\x7F]{5,22} Match any ASCII character 5 - 22 times
$ End of the string
For example:
const regex = /^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$/gmi;
See the regex demo
You may use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[\x00-\x7F]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[\x00-\x7F]{5,22}$/i
If you mean printable ASCII chars are allowed use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[ -~]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[ -~]{5,22}$/i
Details
^ - start of string
(?=[^a-z]*[a-z]) - there must be at least 1 ASCII letter in the string
(?:(?![<>\\'"])[ -~]){5,22} - five to twenty-two occurrences of any printable ASCII char other than <, >, \, ' and " (if [\x00-\x7F] is used, any ASCII char other than the chars in the negated character class)
(?![^<>\\'"]*[<>\\'"]) - no <, >, \, ' and " allowed in the string
$ - end of string.

Regex - printable ASCII that not contain some chars (JS)

I'm trying to find a Regex, that allows printable ASCII chars except - " / \ [ ] : ; | = , + * ? < >
The string length must be 1-25
This will work:
/^[^\\[\]\:\;\|\=\,\/+\*\?<>\"]{1,25}$/
But it will match also non-ASCII chars
You may use
/^(?:(?![:[\];|\\=,\/+*?<>"])[ -~]){1,25}$/
See the regex demo
Details
^ - start of string
(?: - outer grouping to enable matching a quantified lookahead-restricted ASCII char range
(?![:[\];|\\=,\/+*?<>"]) - the next char cannot be one defined in the character class (:, [, ], ;, |, \, =, ,, /, +, *, ?,
<, > and ")
[ -~] - any printable ASCII
){1,25} - 1 to 25 occurrences
$ - end of string

regular expression to match name with only one spaces

I have a string condition in js where i have to check whether name entered in text box contains with one space only.
pattern: name.status === 'full_name' ? /^[a-zA-Z.+-.']+\s+[a-zA-Z.+-. ']+$/ : /^[a-zA-Z.+-. ']+$/
But the above regex matches names ending with 2 spaces also.
I need to match it such that the entered name should accept only one space for a name string. So the name will have only one space in between or at the end.
Two observations: 1) \s+ in your pattern matches 1 or more whitespaces, and 2) [+-.] matches 4 chars: +, ,, - and ., it is thus best to put the hyphen at the end of the character class.
You may use
/^[a-zA-Z.+'-]+(?:\s[a-zA-Z.+'-]+)*\s?$/
See the regex demo
Details
^ - start of string
[a-zA-Z.+'-]+ - 1 or more letters, ., +, ' or -
(?:\s[a-zA-Z.+'-]+)* - zero or more sequences of:
\s - a single whitespace
[a-zA-Z.+'-]+ - 1 or more letters, ., +, ' or - chars
\s? - an optional whitespace
$ - end of string.
Note: if the "names" cannot contain . and +, just remove these symbols from your character classes.
/^\S+\s\S+$/
try this
Some explanations:
^ - start of string
\s - single whitespace
\S - everything except whitespace
"+"- quantifier "one or more"
$ - end of string
you could also use word boundaries...
function isFullName(s) {
return /^\b\w+\b \b\w+\b$/.test(s);
}
['Giuseppe', 'Mandato', 'Giuseppe Mandato']
.forEach(item => console.log(`isFullName ${item} ? ${isFullName(item)}`))

Assistance with Regex regarding limiting alphabetical characters

I am having some trouble setting up RegEx's. Could somebody help me in building a RegEx with these conditions:
Accepts only numbers (0-9)
Accepts a period (.), a negative sign (-), a plus sign (+), a dollar sign ($), and a comma (,)
Does not allow any alphabetical characters (a-z, A-Z)
Try the following expression
[0-9,.$\-\+]
0-9 a single character in the range between 0 (ASCII 48) and 9 (ASCII 57)
,.$ matches a single character in the list ,.$
\- matches the character - literally
\+ matches the character + literally
This regex expression works for me:
^[0-9$.,+-]+$
Explanation:
^ matches the beginning of the string
[] A character set (matches characters listed)
0-9 Any number from 0-9
$ Matches a $ sign
- matches a - sign
. matches a .
, matches a ,
+ matches a + sign
+ One or more of the characters listed
$ End of the string
You can test this here

Categories

Resources