Introduction:
Here I will explain how to check if string contains special characters in jQuery or jQuery check if string contains special characters.
Here I will explain how to check if string contains special characters in jQuery or jQuery check if string contains special characters.
Description:
In previous articles
I explained jQuery slide div from left to right or
right to left,
jQuery enable or disable controls, jQuery Ajax JSON Example in asp.net, jQuery set some time delay for function or method execution,
jQuery cascading dropdownlist example
in asp.net,
jQuery hide div after 5 seconds and many articles
relating to css, jQuery, JavaScript, Asp.net, SQL
server. Now I will explain how to check if
string contains special characters in jQuery.
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. |
|||
|
|||
2 comments :
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?
Hi this is awesome article. Thanks for sharing this with us. really helpful. keep it up.
Note: Only a member of this blog may post a comment.