'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.
Solution 1:[1]
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 ]+
Solution 2:[2]
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]$
Solution 3:[3]
- 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]+)+$
Solution 4:[4]
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]+)*
Solution 5:[5]
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 ]*$/
Solution 6:[6]
This worked for me, simply type in javascript regex validation /[A-Za-z ]/
Solution 7:[7]
This worked for me
/[^a-zA-Z, ]/
Solution 8:[8]
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)
Solution 9:[9]
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 ]*$"
Solution 10:[10]
This will restrict space as first character
FilteringTextInputFormatter.allow(RegExp('^[a-zA-Z][a-zA-Z ]*')),
Solution 11:[11]
This will work for not allowing spaces at beginning and accepts characters, numbers, and special characters
/(^\w+)\s?/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
