sparky
sparky

Reputation: 1

javascript is not working, form is not validating

my login form is not getting validate. javascript is not working. please help me out. I am not getting any alerts.Do Tell me what is wrong with script.

 <script type="text/javascript">
function checkForm(form)
  {
    if(form.username.value == "") {
      alert("Error: Username cannot be blank!");
      form.username.focus();
      return false;
    }
    re = /^\w+$/;
    if(!re.test(form.username.value)) {
      alert("Error: Username must contain only letters, numbers and underscores!");
      form.username.focus();
      return false;
    }

    if(form.npassword.value == "") {
      alert("Error: Username cannot be blank!");
      form.npassword.focus();
      return false;
    }


      re = /[A-Z]/;
      if(!re.test(form.npassword.value)) {
        alert("Error: password must contain at least one uppercase letter (A-Z)!");
        form.npassword.focus();
        return false;
      }

    alert("You entered a valid password: " + form.npassword.value);
    return true;
  }
    </script>

this is login form

<div class="section section_with_padding" id="contact"> 
            <h2>Change Password</h2> 

            <div class="half left">
                <h4>&nbsp;</h4>
                <table width="91%" align='center' cellpadding= "0" cellspacing= "0" class='main_tab'>



    <tr>
     <td class='login_table' align='left'><form action="changepass.php" method="post" onsubmit="return checkForm(this);>
      <table class="login_tab"> 
     <td class='login_table' align='left'><form  method='post'>

            <tr>
                <td colspan="2" class='login_nam' valign='middle'>Change Password</td>
            </tr>
            <tr>
                <td class='login style1'>Username:</td>
                <td class='login'><input type='text' name='username'> </td>
            </tr>
            <tr>
                <td class='login style1'>Old Password:</td>
                <td class='login'><input type='password' name='opassword'> </td>
            </tr>
            <tr>
                <td class='login style1'>New Password:</td>
                <td class='login'><input type='password' name='npassword'> </td>
            </tr>

            <tr>
                <td class='login' colspan='2'><input type='submit' name='submit' value='Change'>
                  </td>

            </tr>
      </table>
      </form>   </td>
    </tr>

</table>              
   </div>

    </div> 

please some one help in validating the login page with errors on not providing any input(it should display respective error message).

Upvotes: 0

Views: 28

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8618

Looks like you have created a nested form. Please delete the second one.

<form action="changepass.php" method="post" onsubmit="return checkForm(this);> /* Form HTML */

<td class='login_table' align='left'>
  <form  method='post'> /* Remove this */

Hope this helps.

Peace! xD

Upvotes: 1

Related Questions