Introduction:
Using Jquery we can search a
given string for a specific word. In this article I will explain how to use jQuery to search a specific word in given
input string.
The
following Method is used to search for specific word in input string.
<script type="text/javascript">
$(function
() {
$('#btnCheck').click(function () {
if
($('#txtword').val().indexOf('search') != -1) {
alert('String
Contains "search" Word');
return
true;
} else
{
alert('String
Does not contains "search" word');
return
false;
}
});
})
</script>
Full Source Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery check if string contains
specific text</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function
() {
$('#btnCheck').click(function () {
if
($('#txtword').val().indexOf('search') != -1) {
alert('String
Contains "search" Word');
return
true;
} else
{
alert('String
Does not contains "search" word');
return
false;
}
});
})
</script>
</head>
<body>
<center>
<fieldset style="width:350px">
<legend>S<strong>earch string For "search" Word using
Jquery</strong></legend>
<div>
<br />
Enter
Text to Search: <input
type="text"
id="txtword" /><br /><br />
<input type="button"
id="btnCheck"
value="Check"
/>
</div></fieldset></center>
</body>
</html>
0 comments:
Post a Comment