2

I have an easy keyup function for class "keyup-an" for keyup validation on a form. There are about 10 fields with this class. However after post, I add fields to the form. But the background of green and red goes away because its not keyup. How do i do something like this each that will color the backgrounds based on this result on page load

jQuery(document).ready(function() {

$('.keyup-an').each(function(index) {

    var inputVal = $(this).val();
    var numericReg = /^[a-zA-Z0-9_ ]{2,30}$/;
    if(!numericReg.test(inputVal)) {
        $(this).css('background', '#FAC3C3');
        $(this).after('<span class="error error-keyup-1">Please use letters or numbers only</span>');
    } 
    else {
        $(this).css('background', 'lightgreen');
    }
});


$('.keyup-an').keyup(function() {
    $('span.error-keyup-1').hide();
    var inputVal = $(this).val();
    var numericReg = /^[a-zA-Z0-9_ ]{2,30}$/;
    if(!numericReg.test(inputVal)) {
        $(this).css('background', '#FAC3C3');
        $(this).after('<span class="error error-keyup-1">Please use letters or numbers only</span>');
    } 
    else {
        $(this).css('background', 'lightgreen');
    }
});
2
  • 2
    Is that .each() loop not already doing what you're asking about? Commented Oct 24, 2012 at 4:58
  • it does and im a noob for not commit a file properly. Commented Oct 24, 2012 at 5:03

3 Answers 3

2

I guess this is what you wanted..

$(document).ready(function() {
    // Each
    $('.keyup-an').each(function() {
        // Validate
        validate(this);
        // Key up
        $(this).keyup(function(){
            // Validate
            validate(this);
        });
    });
});


// Validate Function
function validate(element) {
    var obj = $(element);
    if(!/^[a-zA-Z0-9_ ]{2,30}$/.test(obj.val())) {
        // Invalid
        obj.css('background', '#FAC3C3');
        if(!obj.next().hasClass('error'))
        { obj.after('<span class="error error-keyup-1">Please use letters or numbers only</span>'); }
    } else {
        // Valid
        obj.css('background', 'lightgreen');
        if(obj.next().hasClass('error'))
        { obj.next().remove(); }
    }
}

Demo: http://jsfiddle.net/BerkerYuceer/q2ajM/

Sign up to request clarification or add additional context in comments.

Comments

1
$('.keyup-an').each(function(index) {

    var inputVal = $(this).val();
    var numericReg = /^[a-zA-Z0-9_ ]{2,30}$/;

    if(!numericReg.test(inputVal)) {
        $(this).css('background', '#FAC3C3');
        $(this).after('<span class="error error-keyup-1">Please use letters or numbers only</span>');
    } 
    else {
        $(this).css('background', 'lightgreen');
    }
});

Comments

0

You should define your keyup events like this. By the way, if your elements form are created dinamically, you must bind events with .on()

jQuery(document).ready(function() {

$('.keyup-an').each(function(index) {
    $(this).keyup(function() {
        var inputVal = $(this).val();
        var numericReg = /^[a-zA-Z0-9_ ]{2,30}$/;
        if(!numericReg.test(inputVal)) {
            $(this).css('background', '#FAC3C3');
            $(this).after('<span class="error error-keyup-1">Please use letters or numbers only</span>');
        } 
        else {
            $(this).css('background', 'lightgreen');
        }
    });
});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.