Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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;
       ...
    }

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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,"");
    Last edited by Philip M; 10-15-2012 at 04:04 PM.

    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.

  • #4
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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-16-2012 at 12:50 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.

  • #6
    New Coder
    Join Date
    Oct 2011
    Posts
    77
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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 == " )

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,664
    Thanks
    80
    Thanked 4,641 Times in 4,603 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    oh my ... the .value totally escaped me. There were already more than enough simpler problems :-)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •