0

I have a box for user input, but I need it to be an input area that can be used like a notepad and not just one line.

I made a text area but when I try to regex the value, it does not handle it and ignores the regex.. but an input handles the regex..

Why is this and how can I regex a text area's input?

<div class='container' >

<div class='listtitle' title='Describe Your Items & Give Your Listing as Much Detail As You Can' >Info<br>
<h5>Give Your Listing as Much Detail As You Can.</h5>
</div>

<div id ='infofooter' class='listfooter'>Max Letters: 250</div>

</div><!--container2--->    


<script>


$(document).ready(function () {

      $('#infovalue').keyup(checkinfo);

});

function checkinfo() {


var info = $("#infovalue").val();
var infoReg = /^[a-zA-Z][a-zA-Z0-9-+&%#=?<>()£~_\.*!, ]{3,54}$/;


    if(!infoReg.test(info)) { 
        $("#infofooter").text("0-250 Standard Characters Please!"), $( "#infovalue" ).addClass( "errorclass" ), $( "#infovalue" ).removeClass( "noerrorclass");
                    }  

    if(infoReg.test(info)) { 
        $("#infofooter").text("info Is Good, Thanks!"), $( "#infovalue" ).addClass( "noerrorclass" ), $( "#infovalue" ).removeClass( "errorclass");
                    } 

var infoB = document.getElementById('infovalue');

var regex= lots/ of/ bad/ words/ - /edited /for /obvious /reasons/gi;
infoB.text=infoB.text.replace(regex, "****");   

};

//changed .text to .val .value also to no avail.

</script>
Community
  • 1
  • 1
  • 1
    your using textarea wrong `` https://developer.mozilla.org/en-US/docs/Web/HTML/Element/textarea –  Jun 20 '18 at 23:23
  • iv tried it everyway else, iv just got to the point of frustration now,, i cant regex the textare because textarea dont use a value, so then how do i regex it, iv tried $('#infovalue').text & .value , also the $sellinginfo in the text area nit as a value and the rest, but i cant regex it for what reason i dont know – MelissaHicking Jun 20 '18 at 23:26
  • That last regex with lots of bad words is just something you put there to simplify the code right. Because that's not a regex. If you'd want to match forward slashes you'd have to escape them. F.e. `re = /bla\/bla/g` – LukStorms Jun 20 '18 at 23:32
  • yes...iv got it the right way in editor – MelissaHicking Jun 20 '18 at 23:37
  • If the problem is in the regex, this can help you test it: https://regex101.com/ – geekley Jun 20 '18 at 23:46
  • According to here `.val()` still works for textarea. https://stackoverflow.com/questions/10507294/how-to-get-the-value-of-a-textarea-in-jquery You just need to make sure that your placing the value correctly the comment by smith is correct on how to do it. – hungrykoala Jun 20 '18 at 23:48

2 Answers2

2

There are many problems with your code. Like missing opening/closing quotes, etc. In respect to your attempt to filter/replace "bad words", this is simply no regex syntax.

Try it like that:

regex= /\b(?:lots|bad|words|edited|for|obvious|reasons)\b/gi;

If you do not want substring matches, remove the \b word boundaries from the pattern.

I have also added /r/n to the first regex in the sample below, if this is the issue:

$(document).ready(function () {
      $('#infovalue').keyup(checkinfo);
});

function checkinfo() {

var info = $("#infovalue").val();
var infoReg = /^[a-zA-Z][a-zA-Z0-9-+&%#=?<>\(\)£~_\\.*!, \r\n]{3,54}$/;

    if(!infoReg.test(info)) { 
        $("#infofooter").text("0-250 Standard Characters Please!"), $( "#infovalue" ).addClass( "errorclass" ), $( "#infovalue" ).removeClass( "noerrorclass");
                    }  

    if(infoReg.test(info)) {
        $("#infofooter").text("info Is Good, Thanks!"), $( "#infovalue" ).addClass( "noerrorclass" ), $( "#infovalue" ).removeClass( "errorclass");
                    } 

var infoB = document.getElementById('infovalue');

var regex= /\b(?:lots|bad|words|edited|for|obvious|reasons)\b/gi;
infoB.value=infoB.value.replace(regex, "****"); 

};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container' >

<div class='listtitle' title='Describe Your Items & Give Your Listing as Much Detail As You Can' >Info<br>
<h5>Give Your Listing as Much Detail As You Can.</h5>
</div>
<textarea class='textbox' name='info' id='infovalue' cols="50" rows="10" value="" required >
</input>
<div id ='infofooter' class='listfooter'>Max Letters: 250</div>
</div>
wp78de
  • 18,207
  • 7
  • 43
  • 71
0

Not sure if this is the problem, but you're trying to put something in the value attribute, which I think works for inputs, but not for textarea elements. So maybe this fixes it:

<textarea class='textbox' name='info' id='infovalue' cols="50" rows="10" required ><?php echo $sellinginfo; ?></textarea>

See: https://stackoverflow.com/a/6007262/9638388

geekley
  • 1,231
  • 10
  • 27