The problem is not clear at all (see also my comment to the question). However, a formal answer would be very simple: you see alert message on certain condition just because you programmed it to happen; one can clearly see it in your code. :-)
Now, the problem is: it is the utterly bad design when you mix up the validation with some action taken through validation, such as alert. This kind of code is totally unsupportable. You need to isolate different aspects of your code. In this case, a validation function (any validation, with no exclusion) should only calculate some value, without any side effects
. In this case, you can return, for example, a string describing the validation problem, or null or empty string, in case of successful validation.
An alternative approach would be throwing some exception with the object describing the validation problem (could also be a sting, in the simple case). And successful validation should do nothing. Please see: