How to check the text for the presence of predefined keywords or expressions?

There is a certain <textarea> in it there must initially be some text, then the user enters his own and if that he entered matches the keys that are predefined, we show him the button next.

In general, I sketched two small scripts, but the first works only if one keyword is present, and the second works as it should, but only if the text was entered in advance, and it does not respond to the new text.

First script

 <script type="text/javascript"> function check (id, sbm) { if (document.getElementById(id).value == 'привет') document.getElementById(sbm).style.display = ''; }; </script> <form> <textarea type="text" id="pole"></textarea> <input type="button" value="Проверить" onclick="check('pole','sbm');"> <input type="submit" id="sbm" value="Далее" style="display:none"> </form> 

Second script

  <script> $(document).ready(function (){ $(".check").click(function check(){ $("textarea:contains(привет)").html(function(){$("#next").css("display", "block");}) }); }); </script> 

I would be very grateful for any help, because in JS I have not been strong with this for two days.

    2 answers 2

    In fact, everything is very simple. Referring to the jquery documentation we make the following output or a slight change:

     $( "form" ).submit(function( event ) { var check = $( "input:first" ).val() if ( check == "correct" || check == "ok" || check == "good") { $( "span" ).text( "Validated..." ).show(); return; } $( "span" ).text( "Not valid!" ).show().fadeOut( 1000 ); event.preventDefault(); }); 

    or more swirling option:

     $( "form" ).submit(function( event ) { var check = $( "input:first" ).val() if ( ["correct", "ok", "good"].indexOf($( "input:first" ).val().toLowerCase())!= -1) { $( "span" ).text( "Validated..." ).show(); return; } $( "span" ).text( "Not valid!" ).show().fadeOut( 1000 ); event.preventDefault(); }); 

    In this case, we specify an array of possible values, plus an extra bonus: .toLowerCase ensure that OK == ok .

    Well, the example itself is here .

    • Yes, it is practically what you need, Thank you very much) But it is necessary to make it find among other things the text correct, ok, or good. That is, suppose if you have <textarea> beforehand, do you have everything ok? </ Textarea>, and if a person additionally enters into this text, let's say “good or other key” so that the validation is successful. In general, what would the text be if the keyword was found, then the validation should be successful. - yaslik
    • Why not use placeholder? Let this text be there: '<input placeholde = "Do you have everything ok?">' Clicks on this line, the placeholder disappears (well, or what kind of animation is needed there), and then enters yes / no / ok. I understand this is preparing a questionnaire? I would implement it through placeholder. Updated the code in the example. - NeedHate
    • No, placeholder is not suitable, because it will be English lessons and there will be a text in advance that needs to be supplemented or changed. - yaslik
    • Updated code. So you can try to implement. - NeedHate
    • Thank you, but it is necessary that all the text was in the textarea. By the way, here is a very similar example, only here with the code htmlacademy.ru/courses/4/run/1 - yaslik
     $(function(){ var chk = $('.check'); var check = function(){ var text = 'привет', el = $('.code'), btn = $('#next'), re = new RegExp(text, "gi"); if (re.test(el.val())) { btn.show(); $('.err').hide(); } else { $('.err').show(); btn.hide(); } } chk.click(function(e){ e.preventDefault(); check(); }); check(); });