Introduction: 
Here I will explain JQuery regular expression to allow special characters, spaces and alphanumeric text or regular expression to allow special characters, spaces and alphanumeric characters in JQuery.
Description: 
   
In previous articles I explained jQuery regular expression to validate url without http(s), regular expression to validate phone number in jquery, regular expression to replace all special characters with spaces in c#, regex email validation in c# and many articles relating to validation, JQuery. Now I will explain regular expression to allow special characters and spaces.
In previous articles I explained jQuery regular expression to validate url without http(s), regular expression to validate phone number in jquery, regular expression to replace all special characters with spaces in c#, regex email validation in c# and many articles relating to validation, JQuery. Now I will explain regular expression to allow special characters and spaces.
To
allow special characters and spaces we need to write the regular expression
like as shown below
var re = /^[ A-Za-z0-9_@./#&+-]*$/ 
 | 
 
If
you want see it in complete example need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Regex to allow special characters, spaces and alphanumeric</title> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> 
<script type="text/javascript"> 
$(function() { 
$('#btnValidate').click(function() { 
var txt = $('#txtdesc').val(); 
var re = /^[ A-Za-z0-9_@./#&+-]*$/ 
if (re.test(txt)) { 
alert('Valid
  Text') 
} 
else { 
alert('Please
  Enter Valid Text'); 
return false; 
} 
}) 
}) 
</script> 
</head> 
<body> 
<div> 
Enter Text :<input type="text" id="txtdesc" /> 
<input type="button" id="btnValidate" value="Validate" /> 
</div> 
</body> 
</html> 
 | 
 
Live
Demo
If you want to test enter special characters like !~
Enter Text :
 
 | 
 
| 
 If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.  | 
|||
 
                                        Subscribe by RSS
                                      
 
                                        Subscribe by Email
                                      
 | 
|||
                                        Subscribe by RSS
                                      
                                        Subscribe by Email
                                      
8 comments :
very nice post!
Suresh i have one problem i have to take input from barcode scanner but the textbox should be capable to input multiple barcode values with comma as a separator please help me in this regard
thanks in advance
how to write regex to avoid consecutive occurences of special charchers, like '...', '*-', etc?
Give your Valuable Comments
jQuery regular expression???
isn't it Javascript?
Thanks a lot...I used only the expression...with Regex in c#
Awesome post.Great to visit
Great... It works fine
its not working for square bracket ]
Note: Only a member of this blog may post a comment.