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

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

Feb 4, 2014
Introduction:

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

Description:


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


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Check if string contains special characters or not</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></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.');
}
})
})
</script>
</head>
<body>
<form id="form1">
<div>
Enter Search:<input type="text" id="txtName" />
<input type="button" id="btnClick" value="Check" />
</div>
</form>
</body>
</html>
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 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

2 comments :

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 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.