0

I have a form that I would like all fields to be filled in. If a field is not filled out, I would like to display a red background of the field and an message.

    \$('#id_form').blur(function()
    {
        if( $(this).val().length == 0 ) {
            $(this).parents('p').addClass('warning');
        }

print $q->start_form (-method => 'post', -action => "add.pl", -id => 'id_form');
print $q->legend({-class => "ui-widget-header ui-corner-all"},"");
print $q->input ({-id => 'name', -name => 'name',-type => 'text' , -size => 20, -style => ' margin-left:300 px' },"<br>");

I am not sure this is the right way!

Armida
  • 65
  • 1
  • 3
  • 11

3 Answers3

0

You have to bind your element to the DOM. You can use the $(document).ready(), or use jQuery's anonymous function like this:

$(function () {
    $('#id_form').blur(function () {
        if ($(this).val().length == 0) {
            $(this).parents('p').addClass('warning');
        };
    });
});
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
0

try this http://jsfiddle.net/tYhpw/

$('#id_form input').on('blur', function () {
    if( $(this).val() == '' ) {
        $(this).css('border','1px solid #f00');
    }else{
        $(this).css('border','1px solid #000');
    }
});
David
  • 1,829
  • 20
  • 31
0

I used this Jquery function with a button to check if all fields had been filled in:

    function check(){

    var ok = 1;

    $('input').each(function(){

    if($(this).val().length == 0){ 
        ok = 0; $(this).css('background-color','red');
        alert("Error message"); 
    }

    else{ $(this).css('background-color',''); }
});

if(ok != 1){ alert("Error message"); }

else{ $('#form').submit(); }

}

Maybe this helps?

MickvJ
  • 191
  • 1
  • 1
  • 9