最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - JavaScript regular expression for business name with some validation - Stack Overflow

programmeradmin1浏览0评论

I need JavaScript regular expression for business name with following conditions:

  1. Numbers, spaces and the following characters are allowed:

    ~ ` ? ! ^ * ¨ ; @ = $ % { } [ ] |  /. < > # “ - ‘
    
  2. Should be at least 2 characters one of which must be alpha or numeric character

  3. 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:

  1. Numbers, spaces and the following characters are allowed:

    ~ ` ? ! ^ * ¨ ; @ = $ % { } [ ] |  /. < > # “ - ‘
    
  2. Should be at least 2 characters one of which must be alpha or numeric character

  3. 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!.

Share Improve this question edited Jun 9, 2015 at 8:24 simbabque 54.4k8 gold badges76 silver badges140 bronze badges asked Jun 9, 2015 at 7:59 MaheshMahesh 611 silver badge10 bronze badges 5
  • 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
Add a ment  | 

2 Answers 2

Reset to default 8

You 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 for don't start with space
  • (?!.*\s$) lookahead assertion for don't end with space
  • (?=.*[a-zA-Z0-9]) lookahead assertion for atleast 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.

发布评论

评论列表(0)

  1. 暂无评论