AustinP
AustinP

Reputation: 95

How do i check if a text is almost the same as required

I want to check if the value of my textarea is almost the same as requires , for example :

I have a HTML code :

<textarea class="ab" placeholder="Type here"></textarea>
<div class="result">
</div>

and a Jquery code :

$(document).ready(function(){

  $(".btn").click(function(){
  var a = $(".ab").val();
  var b = $(".result").html();
    /* */



if(a.indexOf('Which of the following is generally true about Open Source software?') >= 0){$('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');}    /* */
    else{
      $(".result").html("error");

    }
  });

}); 

this code doesn't work as what i want actually , this is just what i tried to make . But the thing i want is for example when the value of the $('.ab') is almost the same as the text Which of the following is generally true about Open Source software? like the following is generally true or true about the Open Source , the $(".result") still have the html as Its usually developed on a volunteer basis and anyone is free to change it (correct).

So how do i do that , thanks for your help

Upvotes: 1

Views: 262

Answers (2)

Jaya Vishwakarma
Jaya Vishwakarma

Reputation: 1332

Actually it should be:

    $(document).ready(function(){
        $(".btn").click(function(){
            var a = $(".ab").val();
            var b = $(".result").html();
            var c = 'Which of the following is generally true about Open Source software?';
            console.log(c.indexOf(a));
            if(c.indexOf(a) >= 0){
                $('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');
            } else {
                $(".result").html("error");
            }
        });

    }); 

<textarea class="ab" placeholder="Type here">following is generally true about Open Source</textarea>
<div class="result"></div>
<button class="btn">test</button>

Upvotes: 0

guest271314
guest271314

Reputation: 1

Try splitting input text into array , using $.each() to iterate input words , if input words match at least five words in selected phrase , return true , else return false at if ; e.g.; try typing or pasting at textarea

the following is generally true or true about the Open Source

$(document).ready(function() {

  $(".btn").click(function() {
    var a = $(".ab");
    var b = $(".result");
    var arr = a.val().split(/\s+/);
    var n = 0;
    var phrase = "Which of the following is generally true about Open Source software?";
    $.each(arr, function(key, val) {
      if(phrase.indexOf(val) >= 0) ++n;
    })
  
    if (n >= 5) {
     b.html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');
    }
    else {
      b.html("error");
    };
    a.val(""); n = 0;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<textarea class="ab" placeholder="Type here"></textarea>
<div class="result"></div>
<button class="btn">click</button>

Upvotes: 1

Related Questions