Question

I am trying to validate a form and I want to be able to toggle the error messages off and on when the user inputs a value into the form field. This is what I have so far that is not working:

$('#uTagNum').blur(function() {
    var tagNumber=$(this).val();        
    if (tagNumber.length < 9){
        $('#tagErrorMsg').html('<div>Invalid format.Hover over Tag Number column name to see valid formats</div>');
          $('#uTagNum').blur(function() {
        $('#tagErrorMsg').hide();
    });
});

If the user puts in "Dgfh578" and it is not 9 characters or digits long then I need the tagErrorMsG to appear below the field. If the user deletes what they typed the the error message will disappear unless again they type less then 9 characters or digits.

Was it helpful?

Solution

You have some syntax errors and you probably don't want to use hide(), just clear the HTML for #tagErrorMsg. Have a look at this example - http://jsfiddle.net/jayblanchard/mqeCj/

$('#uTagNum').blur(function () {
    var tagNumber = $(this).val();
    console.log(tagNumber);
    if (tagNumber.length < 9) {
        $('#tagErrorMsg').html('<div>Invalid format.Hover over Tag Number column name to see valid formats</div>');
    } else {
        $('#tagErrorMsg').html('');
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top