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

jQuery - How to Check if String Contains Special Characters or Not

Feb 4, 2014

Here I will explain how to check if string contains special characters in
jQuery or jQuery check if string contains special characters.


To check if string contains special characters we need to write the code like as shown below

<html xmlns="">
<title>jQuery Check if string contains special characters or not</title>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(function () {
$('#btnClick').click(function () {
var str = $('#txtName').val();
if (/^[a-zA-Z0-9- ]*$/.test(str) == false) {
alert('Your String Contains illegal Characters.');
<form id="form1">
Enter Search:<input type="text" id="txtName" />
<input type="button" id="btnClick" value="Check" />
Once we run above query we will get output like as shown below

Live Demo

To check live demo enter text with special characters and click on below button

Enter Search:

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, 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


sakthideveloper said...

hi suresh, in some browser the script is disable by user. in this time the script will not work. Is any way to enable the script programatically?

i searching solution for this. can you help me?

Amit Shee said...

Hi this is awesome article. Thanks for sharing this with us. really helpful. keep it up.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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