The tests below are supposed to fail in such a way that the alert window always pops up. Then someone trying to debug it will alter the pop window logic to prevent the window from appearing when the form is filled in.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<form id="myform" action="javascript:alert( 'success! both form fields are filled in.' );">
<input id="name" name="name" type="text" /><br>
<input id="email" name="email" type="text" /><br>
<input type="submit" value="submit" />
</form>
<script type="text/javascript" >
$("#myform").submit(function (event) {
var messages = "";
if ($("#name").val() == "") {
messages += "Name is required.";
}
if ($("#email").val() == "") {
messages += "Email is required.";
}
if (messages.length != "") {
alert(messages);
}
});
</script>
</body>
However, I do not see the window when both forms are filled in. I though this would fail when messages is empty because the string is not 0, which messages.length reports correct.
If none or only one of the fields are filled in, then I do see the pop up message. But, again, I want it to pop up all the time.
One way more obvious way to make the window not pop up if the form fields have data would be to ask
var messages = "";
.....
if (messages != "0"){
alert(message);
Why doesn't it fail when messages.length is 0?
Does "" equal 0 in javascript?
Is messages.length considered a string, and thus not "0" != ""?
messages.length != 0
should have been on way to correct the mistake. – ndasusers Jul 25 '14 at 13:09