admin管理员组

文章数量:1277393

How to write a regular expression for validating a organisation name which allows Alphanumeric as the starting characters and only special characters like ., -, # and &.

I tried but it's not working

/^[a-z]|\d?[a-zA-Z0-9]?[a-zA-Z0-9\s&@.]+$

Some Valid Names

Hercules.Cycle
Herbal & Product
Wele @ 123

Invalid Names

&Hercules
Colgate!()
.Youtube
@Incule

How to write a regular expression for validating a organisation name which allows Alphanumeric as the starting characters and only special characters like ., -, # and &.

I tried but it's not working

/^[a-z]|\d?[a-zA-Z0-9]?[a-zA-Z0-9\s&@.]+$

Some Valid Names

Hercules.Cycle
Herbal & Product
Wele @ 123

Invalid Names

&Hercules
Colgate!()
.Youtube
@Incule

Share Improve this question edited Jun 28, 2011 at 12:32 Francisco R 4,0481 gold badge24 silver badges37 bronze badges asked Jun 28, 2011 at 10:22 vivekvivek 572 gold badges3 silver badges9 bronze badges 7
  • 1 Please add some samples for which is failing. – Shamim Hafiz - MSFT Commented Jun 28, 2011 at 10:24
  • 2 And how did you try it? What is not working? Please add valid and invalid inputs. – Felix Kling Commented Jun 28, 2011 at 10:25
  • 4 Please provide some examples of valid and invalid names – Rob Cowie Commented Jun 28, 2011 at 10:25
  • 4 Why does an organization name need validating in the first place? What about umlauts and other special characters that might be present in a pany name? – Pekka Commented Jun 28, 2011 at 10:26
  • 3 I have seen organization names with parentheses, Japanese characters, exclamation marks and even question marks. – user142019 Commented Jun 28, 2011 at 11:20
 |  Show 2 more ments

5 Answers 5

Reset to default 6

Is that what you want?

^[A-Z]([a-zA-Z0-9]|[- @\.#&!])*$

You guys can use this below validation ,

As per our requirement from client, "Company Name" can have only 'single space' in between words along with few permitted special characters in it.

/^[a-zA-Z0-9-@.{}#&!()]+(\s[a-zA-Z0-9-@{}.#&!()]+)+(\s[a-zA-Z-@.#&!()]+)?$/

Output:- Correct/Positive Response. These below mentioned outputs are allowed..

  • @arshaa Technologies (m91) HYD
  • @9Arshaa Technologies (HYD) IND
  • #AT&T {IND} HYD
  • #Apple.India {HYD} (INDIA)

Negative/Incorrect response. These below mentioned few outputs will not be allowed, according to above mentioned RegEx. As they contain multiple spaces or Special Characters not in RegEx.

  • @arshaa _Technologies (m91) HYD
  • @9Arshaa --Technologies (HYD) IND
  • #AT&T {IND} HYD.
  • #Apple. -^India {HYD} $(INDIA)

Note: Please feel free to update your answers in ments section for further modification of this RegEx.

I've made and tested the above regular expression and it solves all what you mentioned:

/^[.@&]?[a-zA-Z0-9 ]+[ !.@&()]?[ a-zA-Z0-9!()]+/

Below all checks off.

Some Valid Names

  • Hercules.Cycle
  • Herbal & Product
  • Wele @ 123

Invalid Names

  • &Hercules
  • Colgate!()
  • .Youtube
  • @Incule

Try this pattern: ^\w[\w.\-#&\s]*$ or ^[a-zA-Z0-9][a-zA-Z0-9\.\-#&\s]*$

^[A-Z]([a-zA-Z0-9.-_,]|[- @.#&!])*$

This would allow certain special characters after the first word Sample Names: 1. Hoyt, Gilman & Esp
2. Y 105 Kgfy

本文标签: javascriptRegular Expression for Organisation nameStack Overflow