Nitin Kabra
Nitin Kabra

Reputation: 3226

Regular Expression for alphabets with spaces

I need help with regular expression. I need a expression which allows only alphabets with space for ex. college name.

I am using :

var regex = /^[a-zA-Z][a-zA-Z\\s]+$/;

but it's not working.

Upvotes: 54

Views: 256354

Answers (13)

Vishal Pawar
Vishal Pawar

Reputation: 1737

This works for me

function validate(text) {
  let reg = /^[A-Za-z ]+$/; // valid alphabet with space
  return reg.test(text);
}

console.log(validate('abcdef'));       //true
console.log(validate('abcdef xyz'));   //true
console.log(validate('abc def xyz'));  //true
console.log(validate('abcdef123'));    //false
console.log(validate('abcdef!.'));     //false
console.log(validate('abcdef@12 3'));  //false

Upvotes: 1

Akshatha P Halady
Akshatha P Halady

Reputation: 86

This will restrict space as first character

 FilteringTextInputFormatter.allow(RegExp('^[a-zA-Z][a-zA-Z ]*')),

Upvotes: 0

Muhammad Asad
Muhammad Asad

Reputation: 51

This one "^[a-zA-Z ]*$" is wrong because it allows space as a first character and also allows only space as a name.

This will work perfectly. It will not allow space as a first character.

pattern = "^[A-Za-z]+[A-Za-z ]*$"

Upvotes: 4

Subhomoy halder
Subhomoy halder

Reputation: 41

This worked for me, simply type in javascript regex validation /[A-Za-z ]/

Upvotes: 1

Dileepa Nipun Salinda
Dileepa Nipun Salinda

Reputation: 384

This will work too,

it will accept only the letters and space without any symbols and numbers.

^[a-zA-z\s]+$ 

^ asserts position at start of the string Match a single character present in the list below [a-zA-z\s]

  • matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z matches a single character in the range between a (index 97) and z (index 122) (case sensitive) A-z matches a single character in the range between A (index 65) and z (index 122) (case sensitive) \s matches any whitespace character (equivalent to [\r\n\t\f\v ]) $ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Upvotes: 2

Victor Fiamoncini
Victor Fiamoncini

Reputation: 21

This worked for me

/[^a-zA-Z, ]/

Upvotes: 1

user4739287
user4739287

Reputation:

Regular expression starting with lower case or upper case alphabets but not with space and can have space in between the alphabets is following.

/^[a-zA-Z][a-zA-Z ]*$/

Upvotes: 2

SATYA PRAKASH NANDY
SATYA PRAKASH NANDY

Reputation: 157

  • Special Characters & digits Are Not Allowed.
  • Spaces are only allowed between two words.
  • Only one space is allowed between two words.
  • Spaces at the start or at the end are consider to be invalid.
  • Single word name is also valid : ^[a-zA-z]+([\s][a-zA-Z]+)*$
  • Single word name is in-valid : ^[a-zA-z]+([\s][a-zA-Z]+)+$

Upvotes: 3

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

This is the better solution as it forces the input to start with an alphabetic character. The accepted answer is buggy as it does not force the input to start with an alphabetic character.

[a-zA-Z][a-zA-Z ]+

Upvotes: 35

Koios Kvasir
Koios Kvasir

Reputation: 143

This will accept input with alphabets with spaces in between them but not only spaces. Also it works for taking single character inputs.

[a-zA-Z]+([\s][a-zA-Z]+)*

Upvotes: 5

muruganandham
muruganandham

Reputation: 121

This will allow space between the characters and not allow numbers or special characters. It will also not allow the space at the start and end.

[a-zA-Z][a-zA-Z ]+[a-zA-Z]$

Upvotes: 12

krishna
krishna

Reputation: 1

This will work for not allowing spaces at beginning and accepts characters, numbers, and special characters

/(^\w+)\s?/

Upvotes: -2

Petar Ivanov
Petar Ivanov

Reputation: 93030

Just add the space to the [ ] :

var regex = /^[a-zA-Z ]*$/;

Upvotes: 110

Related Questions