CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   simple validation issue (http://www.codingforums.com/showthread.php?t=277021)

rexhvn 10-15-2012 12:53 PM

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.

devnull69 10-15-2012 01:19 PM

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;
  ...
}


Philip M 10-15-2012 02:59 PM

Quote:

Originally Posted by devnull69 (Post 1280331)
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,"");

rexhvn 10-15-2012 11:16 PM

Quote:

Originally Posted by Philip M (Post 1280366)
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>


Old Pedant 10-16-2012 01:03 AM

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;
      }
  }


rexhvn 10-16-2012 01:33 AM

Quote:

Originally Posted by Old Pedant (Post 1280521)
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 == " )

Old Pedant 10-16-2012 01:42 AM

LOL! DOH on all of us for missing that! Good catch.

devnull69 10-16-2012 07:01 AM

oh my ... the .value totally escaped me. There were already more than enough simpler problems :-)


All times are GMT +1. The time now is 12:28 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.