Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a regular expression that only accepts text characters with spaces allowed

i have a textbox in my asp page that i need to validate to allow user to input only text characters from A-Z , a-z... but with spaces allowed between words..

i have tried the following but it doesn't work(doesn't allow spaces)::

^[a-zA-Z]*$

can i change it to make it allow spaces???

like image 343
Hammam Muhareb Avatar asked Dec 11 '12 05:12

Hammam Muhareb


People also ask

How do you restrict whitespace in regex?

Trimming Whitespace You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.

Can regex include spaces?

Yes, also your regex will match if there are just spaces.

How do you use spaces in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do you restrict special characters in regex?

var nospecial=/^[^* | \ " : < > [ ] { } ` \ ( ) '' ; @ & $]+$/; if(address. match(nospecial)){ alert('Special characters like * | \ " : < > [ ] { } ` \ ( ) \'\' ; @ & $ are not allowed'); return false; but it is not working.


2 Answers

Try this, for regular expression

^[a-zA-Z ]*$
like image 185
senK Avatar answered Nov 14 '22 04:11

senK


You can add the RegEx character \s to your regular expression, allowing spaces, whitespace and line breaks:

^[a-zA-Z\s]*$

Or, like the other posters said, use a space, to only allow spaces:

^[a-zA-Z ]*$
like image 45
Anton Avatar answered Nov 14 '22 03:11

Anton