Pablo Tobar
Pablo Tobar

Reputation: 634

using jquery addclass and removeclass in PHP

I' working on a PHP-Jquery-Ajax submit info form and I would like to use the addclass and removeclass jquery methods but I can't make it work as I want, so my question is: How do I have to fix the code below in order to add a css depending on the user input?

The addclass and removeclass are in the function verificaForm where I validate the input fields, in this case I only show you two fields validations but there are more... I also know that out there is a library(validate.js) that helps to validate the inputs from users but in this specific case I must have to sitck to this code.

here is my js code and thanks in advance:

$(document).ready(function () {

    $('#ID_formulario').on('submit', function (e) {

        e.preventDefault();

        var nombre = $('input#ID_nombre').val().trim();
        var email = $('input#ID_email').val().trim();

        if (validaForm(nombre, email)) {

            $('#result').html("<b>resolviendo peticion...</b>");

            var url = $(this).attr('action');
            var data = $(this).serializeArray();
            var type = $(this).attr('method');

            //...more code goes here ... it works fine ...
        }

    });
});

function validaForm(nombre, email) {

    if ((nombre == '') || nombre.replace(/s+/, '') == '') {
        alert("Favor especificar nombre");
        nombre.addClass('hightlight');
        return false;
    } else {
        else nombre.removeClass('hightlight');
    }

    if (nombre.length < 4) {
        alert('El valor del campo es muy corto');
        return false;
    }
    if ((email == '') || email.replace(/s+/, '') == '') {
        alert("Favor especificar correo");
        return false;
    }
    return true;
}

Upvotes: 0

Views: 1004

Answers (2)

lshettyl
lshettyl

Reputation: 8181

So, you can add classes to a jQuery object and not to a value. Change things around like below.

Replace

var nombre = $('input#ID_nombre').val().trim();
var email = $('input#ID_email').val().trim();

if (validaForm(nombre, email)) {

With

if (validaForm($('input#ID_nombre'), $('input#ID_email'))) {

And modify your function as below.

function validaForm(nombre,email) {

    var nombreVal = $.trim(nombre.val());
    var emailVal = $.trim(email.val());

    if ((nombreVal == '') || nombreVal.replace(/s+/, '') == '') {
    ..........
    ..........
}

And remove that extra else in here:

} else {
    else nombre.removeClass('hightlight');
}

And change it to

} else {
    nombre.removeClass('hightlight');
}

Upvotes: 1

Francesco
Francesco

Reputation: 1413

You should pass the element to the function, not the value. Then You can obtain the value within the function. Something like that:

 var nombre = $('input#ID_nombre');
 var email = $('input#ID_email');

    if(validaForm(nombre, email)) 

    ....


   function validaForm(nombre,email){

   var nombre_value = nombre.val().trim();
   var email_value = email.val().trim();
   .......

Upvotes: 1

Related Questions