0

I have an #error paragraph. Everytime there is an error within the form on submit. The inputs placeholder text gets added to the #error paragraph.

My problem:

It happens everytime a user clicks submit. So the #error message returns:

Please fill in yourfirst name, last name, company, position, first name, last name, company, position, first name, last name, company, position, first name, last name, company, position, first name, last name, company, position, first name, last name, company, position,

I've looked for other solutions and tried this:

if (input.attr('placeholder').indexOf($('#error')) >= 0){
} else{
     $('#error').append(input.attr('placeholder').toLowerCase() + ', ');
}

Is there any way to check if the placeholder text already exists in the #error message? Here's a fiddle. I'm sorry it's so convoluted. But its what i've been working on and had it handy.

Thanks for your help in advance!

isherwood
  • 58,414
  • 16
  • 114
  • 157
Modelesq
  • 5,192
  • 20
  • 61
  • 88
  • 1
    `.indexOf($('#error'))` Why are you passing an object to `indexOf` method? – Ram Mar 13 '13 at 19:48
  • 1
    `input.attr('placeholder').indexOf($('#error'))` will always be `-1` unless your string contains `[object Object]`, maybe you meant `$("#error').text()`? – Kevin B Mar 13 '13 at 19:50
  • can you overwrite the div contents with .html() or .text() each time? – Jake Zeitz Mar 13 '13 at 19:51

4 Answers4

2

http://jsfiddle.net/8YgNT/20/

var errorText = '';

//Validate required fields
for (i = 0; i < required.length; i++) {
    var input = $('#' + required[i]);

    if ((input.val() == "") || (input.val() == emptyerror)) {
        input.addClass("tobefixed");
        errornotice.fadeIn(750);

        if (input.attr('placeholder').indexOf($('#error')) >= 0) {
            // do nothing
        } else {
            errorText = errorText + $(input).attr('placeholder').toLowerCase() + ', ';
        }

        input.val(emptyerror);
        errornotice.fadeIn(750);
    } else {
        input.removeClass("tobefixed");
    }
}

$('#error').html('').append('Please fill in your ' + errorText);
isherwood
  • 58,414
  • 16
  • 114
  • 157
0

I simple add one line in your fiddle and it's working now:

required = ["id_first_name", "id_last_name", "id_firmbox", "id_job_title"];

errornotice = $("#error");
emptyerror = "Please fill out this field.";

$("#startform").submit(function(){
//Validate required fields

$("#error").html("Please fill in your");

for (i=0;i<required.length;i++) {
    var input = $('#'+required[i]);
    if ((input.val() == "") || (input.val() == emptyerror)) {
        input.addClass("tobefixed");        
        errornotice.fadeIn(750);


        // =====Here===== //
        if (input.attr('placeholder').indexOf($('#error')) >= 0){
        } else{
            $('#error').append(input.attr('placeholder').toLowerCase() + ', ');
        }
        // ============== //



        input.val(emptyerror);
        errornotice.fadeIn(750);
    } else {
        input.removeClass("tobefixed");
    }
 }

 if ($(":input").hasClass("tobefixed")) {
    return false;
 } else {
    errornotice.hide();
    return true;
 }
});
$(":input").focus(function(){
 if ($(this).hasClass("tobefixed") ) {
     $(this).val("");
     $(this).removeClass("tobefixed");
 }
});

This line do the all trick:

$("#error").html("Please fill in your");

Saludos ;)

Hackerman
  • 12,139
  • 2
  • 34
  • 45
0

If you want to check whether #error contains the string you're wondering about, you can use

($('#error').text().indexOf(a_string)) != -1

This will return true if #error contains a_string.

There's a longer discussion on searching strings for other strings in this question.

Community
  • 1
  • 1
ckersch
  • 7,507
  • 2
  • 37
  • 44
0

It seems you're doing it the wrong way round (and forgetting to get the text out of the #error element):

if ( $('#error').text().indexOf( input.attr('placeholder') ) >= 0)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375