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?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
user3026704
  • 123
  • 2
  • 3
  • 8
  • 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 – vogomatix Nov 24 '13 at 07:15

5 Answers5

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.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    @user3026704: What Matt said again: *"Remove the `[]`s"*. E.g., `var error = ...` – T.J. Crowder Nov 24 '13 at 06:58
  • if i remove [] how can it be an array? – user3026704 Nov 24 '13 at 06:59
  • 1
    @user3026704 JavaScript isn't PHP. Variable names have nothing to do with types in JavaScript. – Matt Ball Nov 24 '13 at 06:59
  • 1
    @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`. – T.J. Crowder Nov 24 '13 at 06:59
  • 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" – user3026704 Nov 24 '13 at 07:01
  • @user3026704 [someone else](http://stackoverflow.com/a/20171998/139010) has already answered showing you how to do that. This is a **very** basic task with JavaScript; have you done made _any_ attempt at understanding how to use arrays in JavaScript? – Matt Ball Nov 24 '13 at 07:04
  • @user3026704 "i want to create each element of array in different places", use `push` : http://stackoverflow.com/a/20172022/1636522. –  Nov 24 '13 at 07:17
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

vogomatix
  • 4,856
  • 2
  • 23
  • 46
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 = [];
Scherbius.com
  • 3,396
  • 4
  • 24
  • 44
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.

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.

Ashish Jain
  • 760
  • 1
  • 8
  • 23