A regular expression that allows alphanumeric, only one space, and then alpha-numeric

I searched and searched and tried many different ways, but I cannot figure it out. I am looking for a way to resolve alphanumeric characters, and then only one space, then alphanumeric characters. I am sure it is easy, but I do not know it.

Examples of what I want:

  First last allowed
     First la-st not allowed
     FirstLast Not Allowed
     First last not allowed
     First la'st not allowed

I would like to remove invalid characters from a string.

Please let me know if you need more information. Many thanks!

+7
source share
6 answers
^[a-zA-Z0-9]+ [a-zA-Z0-9]+$ 

& hellip; gotta do it.

+13
source

This should do it:

 ^[0-9a-zA-Z]+ [0-9a-zA-Z]+$ 

Demo: http://jsfiddle.net/5m6RH/

+6
source

Use this regex:

 ^[\p{L}\d]+ [\p{L}\d]+$ 
+5
source

I believe that you do not need numbers in the names, and you are looking for this regular expression:

 ^\p{L}+ \p{L}+$ 

or

 ^\p{L}+\s\p{L}+$ 

where \p{L}+ matches one or more characters of the letter, so not only az and az , but also characters from other languages. For example, Elise Wilson.


If you really need only alphanumeric characters, and the input should have two sections with one space between them; and invalid characters must be removed, and then:

  • replace all matches [^\s\dA-Za-z]+ with an empty string,
  • trim leading spaces by replacing ^\s* an empty string,
  • trim trailing spaces by replacing \s*$ an empty string and
  • check / check such a string with the regular expression ^[\da-zA-Z]+ [\da-zA-Z]+$

To exclude numbers from a string, remove \d from previous patterns.

To allow a longer space between, and not just one run character, add + for the space in the first pattern or for \s in the second.

+1
source

This should work

 (?i)\b[az\d]+ [az\d]+\b 
0
source

this will work great for alphanumeric and only one space in between, if applicable

  @"^[a-zA-Z0-9]+([ ]{0,1}[a-zA-Z0-9]+)*$" 

he will allow such values

  anand anand dev anand123 anand123 dev 123anand 123anand dev123 my user name is anand123 anand123 dev **Not allowed due to multiple space** anand dev **not allowed due to space at begining** 

Hope this helps you. Thanks.

0
source

All Articles