Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-15-2012, 12:53 PM   PM User | #1
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
simple validation issue

Hi,

I learning javascript and now doing a simple validation however i'm coming across an issue. See code below

Html
Code:
<form name="loginsubmit" method="post" action="adproperties.html"></form>
Javascript
Code:
   <script>
   // this is for the login validation
   function loginvalidation()
   {
	if(document.loginsubmit.usernameform == '' );
	alert('Please enter your username');
	return false;
   }


   </script>
The issue that i'm having is when the username is submited with no text, the alert will appear, but even when I include text it still appears and doesn't go to the selected link.

Help would be appreciated.
rexhvn is offline   Reply With Quote
Old 10-15-2012, 01:19 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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;
   ...
}
devnull69 is offline   Reply With Quote
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,044
Thanks: 197
Thanked 2,412 Times in 2,390 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
Old 10-15-2012, 11:16 PM   PM User | #4
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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,"");
I still can't manage to get it to work. Nothing occurs with the below code.

Code:
<script>
   // this is for the login validation
   function loginvalidation()
	if(document.loginsubmit.usernameform == '' ){
	alert('Please enter your username');
	return false;
   }
   </script>

Last edited by rexhvn; 10-15-2012 at 11:50 PM..
rexhvn is offline   Reply With Quote
Old 10-16-2012, 01:03 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You are missing the closing right brace } for the left brace { that follow the if.

You are also missing the left brace { for beginning the function.

Code:
   function loginvalidation()
   {
       if(document.loginsubmit.usernameform == '' )
       {
          alert('Please enter your username');
          return false;
       }
   }
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-16-2012, 01:33 AM   PM User | #6
rexhvn
New Coder

 
Join Date: Oct 2011
Posts: 49
Thanks: 0
Thanked 0 Times in 0 Posts
rexhvn is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
You are missing the closing right brace } for the left brace { that follow the if.

You are also missing the left brace { for beginning the function.

Code:
   function loginvalidation()
   {
       if(document.loginsubmit.usernameform == '' )
       {
          alert('Please enter your username');
          return false;
       }
   }
It didn't seem to work, however when i added .value it works.

Thank you.
Code:
if(document.loginsubmit.usernameform.value == " )
rexhvn is offline   Reply With Quote
Old 10-16-2012, 01:42 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! DOH on all of us for missing that! Good catch.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-16-2012, 07:01 AM   PM User | #8
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
oh my ... the .value totally escaped me. There were already more than enough simpler problems :-)
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:38 AM.


Advertisement
Log in to turn off these ads.