View Single Post
Old 10-15-2012, 02:59 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by devnull69 View Post
The semicolon in Javascript is used to end a statement. So with the semicolon at the end of the if statement you are already closing the if statement. So no matter what the condition in the if statement is, the next line(s) will always be executed.

This is the correct syntax for a simple if statement (one statement)
Code:
if(condition)
   statement;
This is the correct syntax for an if statement that should include more than one statement
Code:
if(condition) {
   statement1;
   statement2;
   ...s
}
FWIIW, I recommend always using the braces even if the if statement has only one line/statement.

Note that even a single space will pass the validation if(document.loginsubmit.usernameform == '' ) {

You should always strip leading and trailing spaces from all user input:-

Code:
x = x.replace(/^\s+|\s+$/g,"");
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-15-2012 at 03:04 PM..
Philip M is offline   Reply With Quote