¿Keyup if declaración para indexOf string actuando extraño?

I've got a small problem with the following code:

$('#body').keyup(function(){
    var i = $('#body').val();

    if(i.toLowerCase().indexOf("contact") >= 0){
        $('#tosviolation').fadeIn('fast'); 
        $('#sendForm').attr('action','send.php?f=1');
        $('.toscontent').html("TOS Breach!");
    } else { 
        $('#tosviolation').fadeOut('fast'); 
        $('#sendForm').attr('action','send.php?f=0');
    }

    if(i.toLowerCase().indexOf("@") >= 0){
        $('#tosviolation').fadeIn('fast'); 
        $('#sendForm').attr('action','send.php?f=1');
    } else { 
        $('#tosviolation').fadeOut('fast'); 
        $('#sendForm').attr('action','send.php?f=0');
    }
});

Comprueba si #body (textarea) contains the values "contact" or/and "@", and if this textarea contain said values, it pops up / flashes a div telling the user that they're violating the TOS. With the "@", the box stays there when the user types it, yet when the user types "contact", the box just flashes a few times and goes away, I'm guessing since "@" is 1 char long, and "contact" is 7 chars long, it's something to do with that.

Then again, this is quite strange? I was intending it to just fadeIn() and stay faded in. I didn't mind the flash, because of course, on keyup it'll have to run the function every time the user taps a key, so that's fine, but I can't figure out how to keep it there when the user types "contact" somewhere in the textarea. Also, the other part where the it's using the .attr() is directing the user to send.php, if it does contain "contact","@" -- Then it pushes them to send.php?f=1, and if not, then send.php?f=0 -- #sendForm is the ID of a form tag I have earlier, I assume this already works fine.

Does anyone know how to get the box to stay there once it's pulled up with the data "contact" ?

preguntado el 02 de febrero de 12 a las 10:02

3 Respuestas

Try this. Remember, setting the attribute of a form and relying on it for server-side processing is mas peligroso - the end user can easily fiddle with this before posting. You should validate again with the server and not use any GET variables sent with the form.

$('#body').keyup(function () {
    var i = $('#body').val();
    if (i.toLowerCase().indexOf("contact") >= 0 || i.toLowerCase().indexOf("@") >= 0) {
        $('#tosviolation').fadeIn('fast');
        $('#sendForm').attr('action', 'send.php?f=1');
        $('.toscontent').html("TOS Breach!");
    } else {
        $('#tosviolation').fadeOut('fast');
        $('#sendForm').attr('action', 'send.php?f=0');
    }
});

Respondido 02 Feb 12, 14:02

The problem is that when you're typing "contact", you don't have the "@" symbol. So your first if statement for contact evaluates to true, and fades in #tosviolation. However, the next if statement evaluates to false, so it fades it back out straight away. You'll need to restructure your if statements (likely using an OR condition, rather than two separate if statements) to avoid this.

Algo como esto debería funcionar:

$('#body').keyup(function() {
    var i = $('#body').val();
    var contact = i.toLowerCase().indexOf("contact");
    var atsymbol = i.toLowerCase().indexOf("@");
    if (contact >= 0 || atsymbol >= 0) {
        $('#tosviolation').fadeIn('fast');
        $('#sendForm').attr('action', 'send.php?f=1');
        if(contact >= 0) {
            $('.toscontent').html("TOS Breach!");
        }
    } else {
        $('#tosviolation').fadeOut('fast');
        $('#sendForm').attr('action', 'send.php?f=0');
    }
});

Respondido 02 Feb 12, 14:02

AHH. Thanks! I just noticed! Cheers! :) - unicornication

  1. Why don't you just check this once, when the field loses focus or before formsubmit? This is easier and cleaner then checking on every keyup event.

  2. I strongly recommend to check the Input on serverside again. simply adding f=1 or f=0 can be manipulated easily. Also think about what happened if the user disabled js.

Respondido 02 Feb 12, 14:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.