1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Jquery Simple Operation Problem

Discussion in 'jQuery' started by GrassHopper31, Feb 6, 2014.

  1. #1
    Hey! first of all im still new to JavaScript and its libraries, so any help is really appreciated. Anyways, I'm adding a client side validation form for my php mail script so that users dont need to wait to submit the form then refill what they did wrong. Instead, they will be corrected as soon as they fill each one of the forms. My problem is that when input[name=name] has a length of 5 or more it still triggers the css() function. Why is this not working. when i = 8 meaning that 8<4 = false but the function still changes the css. Please help.

    
    $(function(){ //Ready Document Function
        var name = $('[name="name"]');
        var email = $('[name="email"]');
        var subject = $('[name="subject"]');
        var message = $('[name="message"]');
        var sec = $('[name="sec"]');
       
        var div = $("#jquery"); //test code
        //var value = name.attr('value').length; //test code
        div.text('test'); //test code
    
        name.blur(function() {
            var i = $(this).val().length; 
            if(i<4){ //blank or not letters or less than 4
                name.css('background','red');
                div.text(i +'is more than 4');
            }
           
        });
    
        email.on('blur', function() {
        });
       
        message.on('blur', function() {
        });
       
        subject.on('blur', function() {
        });
        sec.on('blur', function() {
        });   
    });
    
    Code (markup):
     
    GrassHopper31, Feb 6, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    have you done any debugging?

    Use chrome go into the developer console then change the code to this

     name.blur(function() {
            var i = $(this).val().length;
            console.log($(this).val());
            if(i<4){ //blank or not letters or less than 4
                name.css('background','red');
                div.text(i +'is more than 4');
            }
          
        });
    HTML:
    Check if the text that you entered into the textbox is coming up in the console. If not then you got the selector wrong.
     
    stephan2307, Feb 20, 2014 IP