1

Hello I'm trying to create an array of errors, and display them at once. Something like this.

if (!first_name) {
    var error[] = "Заполните Фамилию";
    $('#first_name').addClass('error');
} else {
    $('#first_name').removeClass('error');
}

if (!second_name) {
    var error[] = 'Заполните Имя';
    $('#second_name').addClass('error');
} else {
    $('#second_name').removeClass('error');
}
if (!last_name) {
    var error[] = 'Заполните Отчество';
    $('#last_name').addClass('error');
} else {
    $('#last_name').removeClass('error');
}
if (!course) {
    var error[] = 'Заполните Курс';
    $('#course').addClass('error');
} else {
    $('#course').removeClass('error');
}
if (!math && !programming && !english && !history) {
    var error[] = 'Заполните хотябы один предмет';
    $('#math,#programming,#english,#history').addClass('error');
} else {
    $('#math,#programming,#english,#history').removeClass('error');
}

and then

if(error.length > 0) {
    $(".errors").html(error);
}

But i'm getting an error Uncaught SyntaxError: Unexpected token ]

What am i doing wrong?

1
  • I've put a more detailed answer down below, but you would probably have found all these yourself by running the code through a Javascript syntax checker or by loading the page up into a browser with the Javascript debugger activated. If you have Firefox, get the Firebug plugin as its much better than the built in debugger Commented Nov 24, 2013 at 7:15

5 Answers 5

3

Two main problems - the error array was being repeatedly and incorrectly declared, and the display of the resulting array was being handled incorrectly. Here's a fix for both problems....

var error = [];  // initialise empty array
if (!first_name) {
    error.push( "Заполните Фамилию");
    $('#first_name').addClass('error');
} else {
    $('#first_name').removeClass('error');
}

if (!second_name) {
    error.push( 'Заполните Имя');
    $('#second_name').addClass('error');
} else {
    $('#second_name').removeClass('error');
}
if (!last_name) {
    error.push('Заполните Отчество');
    $('#last_name').addClass('error');
} else {
    $('#last_name').removeClass('error');
}
if (!course) {
    error.push( 'Заполните Курс');
    $('#course').addClass('error');
} else {
   $('#course').removeClass('error');
}
if (!math && !programming && !english && !history) {
    error.push( 'Заполните хотябы один предмет');
    $('#math,#programming,#english,#history').addClass('error');
} else {
    $('#math,#programming,#english,#history').removeClass('error');
}

// you will need to join the elements together somehow before displaying them
if (error.length > 0) {
    var data = error.join( '<br />');
    $(".errors").html(data);
}

You might also want to look at using the toggleClass function instead of add/remove, but that's up to you

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

Comments

3

All of these lines contain syntax errors:

var error[] = ...

because error[] is not a valid JavaScript identifier. Remove the []s. The closest valid variable name would be error instead of error[].

This kind of error is made painfully evident when you run your code through a JavaScript linter tool.

7 Comments

@user3026704: What Matt said again: "Remove the []s". E.g., var error = ...
if i remove [] how can it be an array?
@user3026704 JavaScript isn't PHP. Variable names have nothing to do with types in JavaScript.
@user3026704: It isn't, you're assigning a string to it. If you want an array with a single entry, then var error = ["string here"];. The [...] (as the value, not as part of the name) indicates an array, the "string here" is the initial entry within it. var a = [1, 2, 3]; creates an array with three entries and assigns it to the variable a.
Yes, but i want to create each element of array in different places. I mean - if first input is empty - error[0] = 'please fill the first input"
|
1

You are confusing JavaScript with PHP.

This is incorrect way to declare an array:

var error[] = 'Заполните Отчество';

rather:

var error = new Array();

or

var error = [];

Comments

1

To append values into an array using javascript :

var error = [];
error.push('Error 1');
error.push('Error 2');

Then, to display them :

$('.errors').html(
    error.join('<br/>') // "Error 1<br/>Error 2"
);

Doc : push, join.

Comments

0

You can display all error message at once like that

var error=''
if (!first_name) {
    error += "Заполните Фамилию.<br />";
    $('#first_name').addClass('error');
} else {
    $('#first_name').removeClass('error');
}

if (!second_name) {
    error += 'Заполните Имя<br />';
    $('#second_name').addClass('error');
} else {
    $('#second_name').removeClass('error');
}

if (!last_name) {
    error += 'Заполните Отчество<br />';
    $('#last_name').addClass('error');
} else {
    $('#last_name').removeClass('error');
}

if (!course) {
    error += 'Заполните Курс<br />';
    $('#course').addClass('error');
} else {
    $('#course').removeClass('error');
}

if (!math && !programming && !english && !history) {
    error +='Заполните хотябы один предмет<br />';
    $('#math,#programming,#english,#history').addClass('error');
} else {
    $('#math,#programming,#english,#history').removeClass('error');
}

if (error != '') {
    $(".errors").html(error);
    return false;
}

error is a one variable where i stored all the error and display at once on the screen.

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.