Jitender
Jitender

Reputation: 7971

indexOf method in jquery?

i have lost of special character in var name"str" now i want user enter character within it then alert him "hiii" else "byeee" depending on the Indexof check of the string

<head>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
    $('#me').click(function(){
    var Amd=$('.text').val();

    var str="[,],{,},<,>"

    if(Amd.indexOf(str)>-1){

        alert('hiiiii')

        }       
    else {
        alert('byee')
        }
    })
    })
</script>
</head>
<body>
<input type="text" class="text" />
<input type="button" value="click" id="me"/>
</body>

Upvotes: 0

Views: 2164

Answers (3)

Ponmalar
Ponmalar

Reputation: 7031

Check with this.

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  span { color:red; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){

    $('#me').click(function(){
    var Amd=$('.text').val();

    var str="\[,],{,},<,>";
    var n=Amd.indexOf(str);
    if(n>-1)
    {
       alert("Hi");
    }
    else
       alert("Bye");

    });
    });
</script>
</head>
<body>
<input type="text" class="text" />
<input id="me" value="Click Me" type="button" />

</body>
</html>

If you want to check any symbols like " or { or [ anything within the double quotes(""), include \ before that symbols.

Upvotes: 0

xdazz
xdazz

Reputation: 160833

You could use regex match to do this, by the way, you should add ; for every end of line, which is a good habit.

$(function () {
    $('#me').click(function () {
        var Amd = $('.text').val();
        if (Amd.match(/[\[\]{}<>]/)) {
            alert('hiiiii');
        } else {
            alert('byee');
        }
    });
});

Upvotes: 3

jbrtrnd
jbrtrnd

Reputation: 3833

Use RegExp :

if( Amd.match(new RegExp(/[\[\]\{\}<>]/)) ) {
....

Upvotes: 1

Related Questions