1

image:

enter image description here

result:

enter image description here

I have a problem that the submit button (reply button) is not disabled after i press '(single quote) or many of them, I just want it disabled. The point is preventing someone type a single/double quote or a weird character on the textarea. I have a code from someone in here and it works in testing in a different file, but here the condition is different, the form is inside an ajax and I'm very newbe in Jquery, and i don't know where to put that code to make it works.

this is my simple code of ajax:

var x;
function clickreply(obj){
    var varid = obj.id;
    var getnumb = varid.match(/\d/g); //get number from string
    var idreply = 'idreply'+getnumb;
    console.log(varid);
    console.log(getnumb);
    console.log(idreply);
    $.ajax({
        url: '/logincheckmember2.php', //This is the current doc
        type: "POST",
        dataType:'json', // add json datatype to get json
        success : 
        function (result) {
            x=result['ssloginmember'];
            console.log(x);
            if(result['ssloginmember']==null){
                msgBLshow();
            }else{
                x="<center>"+
                    "<form method='post' name='myForm' id='myForm'>"+
                        "<textarea id='tareply' rows='4' cols='50' maxlength='250' placeholder='maxlength=250'></textarea><br>"+
                        "<input type='submit' id='submitreply' value='reply' onclick='clicktareply()'></input>"+
                    "</form>"+
                "</center>";
                document.getElementById("msgcontent1").innerHTML=x;
                $("#msg1").fadeTo(1000, 1);
            }                   
        }
    });
}
function clicktareply(){
    alert(document.getElementById("tareply").value);
}

$(document).ready(function() {
    $('#tareply').keyup(function() {
        if(!$(this).val().match(/^(?!\s)([a-zA-Z0-9 _.)?&]){1,}$/g)) {
           $('#submitreply').prop('disabled', true);
        }else{
           $('#submitreply').prop('disabled', false);
        }
    });
});
Juna serbaserbi
  • 205
  • 2
  • 12
  • A JSFiddle or similar would be helpful – sjm Nov 10 '15 at 03:32
  • ok, i'm trying to make it. – Juna serbaserbi Nov 10 '15 at 03:33
  • You can link your js-file in the header of the HTML page you whant the javascript to be running at. – Zorken17 Nov 10 '15 at 08:36
  • Thank you Zorken17, it already answered by @coolguy. You've helping me a lot. Thank you very much Zorken17. – Juna serbaserbi Nov 10 '15 at 08:39
  • 1
    But I still say use keyup insted if keypress becouse otherwise you will not get the correct result for every occation. – Zorken17 Nov 10 '15 at 12:19
  • ok Zorken17, i will use keyup, according to your suggestions. – Juna serbaserbi Nov 10 '15 at 12:56
  • I have changed from "keypress keydown" to "keyup", it worked also Zorken17, great. – Juna serbaserbi Nov 10 '15 at 13:05
  • wait a minute, woowww, after i see more clearly, you are right Zorken17. I prefer choose **keyup**, it is more suitable for my problem. You are very clever Zorken17. – Juna serbaserbi Nov 10 '15 at 13:15
  • Thanks, nice that you see the difference. But then I don't understand why my other answer didn't work. – Zorken17 Nov 10 '15 at 21:13
  • I didn't know either Zorken, first I just copied your code to the real file not tested file anymore, but I didn't know, It's not worked again, that's why i called you in the previous thread, then @coolguy gave me 1 shot then baaaammm, it worked. I still kept coolguy code but i just changed from **keypress keydown** to *keyup* as your suggestion, then perfect. Actually, Zorken17, if i had a problem again, can i call you again to resolving my debug? because i don't have any teacher here. – Juna serbaserbi Nov 11 '15 at 04:48

1 Answers1

2

you have to use keypress instead of keyup .because you want to prevent the singlequotes

$(document).ready(function() {
    $('body').delegate('#tareply','keyup',function() {
     //match condition ..do your logic here ..personally i use indexOf rather than regexp
        if(!$(this).val().match(/^(?!\s)([a-zA-Z0-9 _.)?&]){1,}$/g)) {
           $('#submitreply').prop('disabled', true);
        }else{
           $('#submitreply').prop('disabled', false);
        }
    });
});
coolguy
  • 7,866
  • 9
  • 45
  • 71
  • it works man, thanks a lot @coolguy and +1 for you, thanks again. – Juna serbaserbi Nov 10 '15 at 03:43
  • 1
    @coolguy I don't see whay you need to use keypress insted of keyup that only delays the check one buttonpush. That means if the submitbutton is grayedout and you push a correct key the button will still be grayedout until you push another correct key. It's working perfectly here in my awnser: http://stackoverflow.com/questions/33610500/i-cant-find-the-simplest-of-textarea-validation/33612389?noredirect=1#comment55019035_33612389 – Zorken17 Nov 10 '15 at 08:26
  • keypress is checked before keyup @Zorken17 – coolguy Nov 10 '15 at 10:23
  • 1
    Thats the problem, it is like keydown it reacts when the key is pressed down. But the if statment checks the inputbox value and not the key, so if you for example set a wrong char in the inputbox the submitbutton will be grayedout and when you delete everything and start writing the submitbutton will be grayed out untill you hit the second char. So your code will not work as intended. @coolguy – Zorken17 Nov 10 '15 at 10:32
  • I have changed from "keypress keydown" (@coolguy suggestion) to "keyup", it worked also Zorken17. It worked of them, great Job. – Juna serbaserbi Nov 10 '15 at 13:07
  • woowww, after i see more clearly, you are right Zorken17. I prefer choose **keyup**, it is more suitable for my problem. You are very clever Zorken17. – Juna serbaserbi Nov 10 '15 at 13:15
  • I give +1 for all your information Zorken17, great. – Juna serbaserbi Nov 10 '15 at 13:17
  • @Coolguy please helping me again in [here](http://stackoverflow.com/questions/33669459/confusing-about-this-cookies-in-redirecting-system) – Juna serbaserbi Nov 12 '15 at 12:23