Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Regex to Allow Special Characters, Spaces and Alphanumeric in jQuery

Mar 5, 2014
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.

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 RSS subscribe by email Subscribe by Email

4 comments :

Rajesh Nair said...

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

Anonymous said...

how to write regex to avoid consecutive occurences of special charchers, like '...', '*-', etc?

Anonymous said...

Give your Valuable Comments

Anonymous said...

jQuery regular expression???
isn't it Javascript?

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.