I made a regular expression that only accepts letters. I'm not really good in regex, thats why I don't know how to include spaces in my regex.
My HTML:
<input id="input" />
My js / jQuery code:
$('#input').on('keyup', function() { var RegExpression = /^[a-zA-Z]*$/; if (RegExpression.test($('#input').val())) { } else { $('#input').val(""); } });
\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.
Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.
Regex uses backslash ( \ ) for two purposes: for metacharacters such as \d (digit), \D (non-digit), \s (space), \S (non-space), \w (word), \W (non-word). to escape special regex characters, e.g., \. for . , \+ for + , \* for * , \? for ? .
A more accurate wording for \W is any Non-Alphanumeric character. \s is for Any Whitespace. Show activity on this post. \W means "non-word characters", the inverse of \w , so it will match spaces as well.
use this expression
var RegExpression = /^[a-zA-Z\s]*$/;
for more refer this http://tools.netshiftmedia.com
$('#input').on('keyup', function() { var RegExpression = /^[a-zA-Z\s]*$/; ... });
\s
will allow the space
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With