gyula
gyula

Reputation: 257

is there something wrong with my if statement?

I am checking the number of wrong lines with a for loop

var lines = dateinput.value.split(/\r?\n/);
var wrongLines ="";
for(var i = 0; i<lines.length ; i++){
    if (lines[i].match(regex) == null) {
        wrongLines += i + 1 +",";
    }

and I want to add different alerts for the number of the wrong lines

if (i = 1 ) {
alert('The date on line ' + wrongLines + ' is invalid. Please enter a valid date formatted DD/MM/YYYY');
}
else if (i > 1 ) {
alert('Dates on line ' + wrongLines + ' are invalid. Please enter a valid date formatted DD/MM/YYYY');
}

but it does not works - every time I get the first alert

Upvotes: -1

Views: 44

Answers (2)

marco.marinangeli
marco.marinangeli

Reputation: 901

at first impression, you should replace if (i = 1) with if (i == 1)

Upvotes: 0

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

if (i == 1 )

You are assigning values instead of comparing.

N.B. :

= means assigning 

== means comparing

=== means strict comparing

Upvotes: 2

Related Questions