I need JavaScript regular expression for business name with following conditions:
Numbers, spaces and the following characters are allowed:
~ ` ? ! ^ * ¨ ; @ = $ % { } [ ] | /. < > # “ - ‘
Should be at least 2 characters one of which must be alpha or numeric character
- No preceding or trailing spaces
examples: Test1
, Ta
, A1
, M's
, 1's
, s!
, 1!
I tried following (for time being I used only 3 special characters for testing purpose):
^(?=(?:[^\A-Za-z0-9]*[\A-Za-z0-9]){2})[~,?,!]*\S+(?: \S+){0,}$
But it doesn't validate s!
or 1!
.
I need JavaScript regular expression for business name with following conditions:
Numbers, spaces and the following characters are allowed:
~ ` ? ! ^ * ¨ ; @ = $ % { } [ ] | /. < > # “ - ‘
Should be at least 2 characters one of which must be alpha or numeric character
- No preceding or trailing spaces
examples: Test1
, Ta
, A1
, M's
, 1's
, s!
, 1!
I tried following (for time being I used only 3 special characters for testing purpose):
^(?=(?:[^\A-Za-z0-9]*[\A-Za-z0-9]){2})[~,?,!]*\S+(?: \S+){0,}$
But it doesn't validate s!
or 1!
.
- Allowed characters: (~), (`), (?), (!), (^), (*), (¨), (;), (@), (=),($), (%), ({), (}), ([), (]), (|), (), (/). (<), (>), (#), (“), (.), (,). (-). (‘) – Mahesh Commented Jun 9, 2015 at 7:59
- Did you try anything? Please share your attempts. – Wiktor Stribiżew Commented Jun 9, 2015 at 8:02
- I tried following (for time being I used only 3 special characters for testing purpose) : ^(?=(?:[^\A-Za-z0-9]*[\A-Za-z0-9]){2})[~,?,!]*\S+(?: \S+){0,}$ but it doesn't validate s! or 1! – Mahesh Commented Jun 9, 2015 at 8:08
- Condition is that one character has to be alphanumeric or the first character has to be alphanumeric? for example, would "!s" be a valid name? – valepu Commented Jun 9, 2015 at 8:20
- !s it's valid. because condition is at least 2 characters one of which must be alpha OR numeric character. – Mahesh Commented Jun 9, 2015 at 8:24
2 Answers
Reset to default 8You can use the following to validate:
^(?!\s)(?!.*\s$)(?=.*[a-zA-Z0-9])[a-zA-Z0-9 '~?!]{2,}$
And add all the characters tht you want to allow in [a-zA-Z0-9 '~?!]
See DEMO
Explanation:
^
start of the string(?!\s)
lookahead assertion fordon't start with space
(?!.*\s$)
lookahead assertion fordon't end with space
(?=.*[a-zA-Z0-9])
lookahead assertion foratleast one alpha or numeric character
[a-zA-Z0-9 '~?!]
characters we want to match (customize as required){2,}
match minimum 2 and maximum any number of characters from the previously defined class$
end of the string
/^(?=(?:[^\A-Za-z0-9][\A-Za-z0-9]){2})[~,?,!]\S+(?: \S+){0,}$/;
matches strings that begin with a special character, followed by a word that contains at least two non-alphanumeric characters, followed by an optional space and another word.