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?
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?
Share Improve this question edited Nov 24, 2013 at 6:56 Tushar Gupta - curioustushar 57.1k24 gold badges106 silver badges109 bronze badges asked Nov 24, 2013 at 6:53 user3026704user3026704 1232 gold badges3 silver badges8 bronze badges 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 – vogomatix Commented Nov 24, 2013 at 7:15
5 Answers
Reset to default 3Two 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
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.
You are confusing JavaScript with PHP.
This is incorrect way to declare an array:
var error[] = 'Заполните Отчество';
rather:
var error = new Array();
or
var error = [];
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.
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.