password and confirm password validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahilar12
    New Member
    • Feb 2009
    • 27

    password and confirm password validation

    hi all,

    i wanted to check whether the password entered matches with the confirm password.either through javascript or through php.i have enclosed my code somebody help on this

    Code:
    <html>
    
    <?include 'menu.php'?>
    <head>
    <script type="text/javascript">
    function checkform()
    {
      var cfname,cstatus,cuname,cpwd,cconfirmpwd;
      with(window.document.createuserform)
      {
    	
    	cfname=fname;
    	cstatus=status;
    	cuname=uname;
    	cpwd=pwd;
    	cconfirmpwd=confirmpwd;
    		
      }
    
    	
    	 if(trim(cfname.value)=="")
    	{
    		alert("please enter the first name");
    		cfname.focus();
    		return false;
    	}
    
    	else if(trim(cstatus.value)=="")
    	{
    		alert("please enter the status");
    		cstatus.focus();
    		return false;
    	}
    
        else if(trim(cuname.value)=="")
    	{
    		alert("please enter the user name");
    		cuname.focus();
    		return false;
    	}
    
    	else if(trim(cpwd.value)=="")
    	{
    		alert("please enter the password");
    		cpwd.focus();
    		return false;
    	}
    
        else if(trim(cconfirmpwd.value)=="")
    	{
    		alert("confirm the password");
    		cconfirmpwd.focus();
    		return false;
    	}
    
       
    	else
        {
    		
    		 cfname.value=trim(cfname.value);
    		 cstatus.value=trim(cstatus.value);
    		 cuname.value=trim(cuname.value);
    		 cpwd.value=trim(cpwd.value);
    		 cconfirmpwd.value=trim(cconfirmpwd.value);
    		
    		 
    	}
    
    		
    	function trim(str)
    	{
    
    		return str.replace(/^\s+|\s+$/g,'');
            
        }
    
    	 
    	
    }
    
    	
    
    </script>

    Code:
    </head>
    <center><h3>CREATE USER</h3></center>
    
    <body>
    
    <form name="createuserform" action="insertuser.php" method="POST" onsubmit="return checkform()">
    
    <table>
    <td>
    <table border="0" cellpadding="0" cellspacing="0">
    
      <tr>
    	<td>
    		<table  border="0" cellpadding="0" cellspacing="0">
    			<tr>
    				<td style="padding-left:20px;padding-bottom:20px;"><a href="createuser.php" style="text-decoration:none"><input type="button" name="new" value="CREATE USER"  style="width:170px;background-image:url(pink.gif)"></a></td>
    			</tr>
    
    			<tr>
    				<td style="padding-left:20px;padding-bottom:20px;"><a href="users.php" style="text-decoration:none"><input type="button" name="new" value="USERS"  style="width:170px;background-image:url(pink.gif)"></a></td>
    			</tr>
    
    			<tr>
    				<td style="padding-left:20px;"><a href="reassignrecords.php" style="text-decoration:none"> <input type="button" name="new" value="REASSIGN RECORDS"  style="width:170px;background-image:url(pink.gif)"></a></td>
    			</tr>
    
    				
    		</table>
    	</td>
    	<td>
    <TABLE cellpadding="5" cellspacing="6" align="center" with frame="box">
    
    <tr>
    
    
    
    <th>First Name<font color="red">*</font></th>
    <td><input type="text" name="fname">
    <th>Last Name</th>
    <td><input type="text" name="lname">
    <th>Status</th>
    <td><select name="status">
    <option>Active</option>
    <option>Inactive</option>
    </select>
    </td>
    
    </tr>
    
    <tr>
    
    <th>User Name<font color="red">*</font></th>
    <td><input type="text" name="uname">
    <th>Password<font color="red">*</font></th>
    <td><input type="password" name="pwd" >
    <th>Confirm Password<font color="red">*</font></th>
    <td><input type="password" name="confirmpwd">
    
    
    </tr>
    
    <tr>
    
    <th>Title</th>
    <td><input type="text" name="title">
    <th>Department</th>
    <td><input type="password" name="dept" >
    <th>Phone</th>
    <td><input type="password" name="phone">
    
    
    </tr>
    
    <tr>
    
    <th>Address</th>
    <td><input type="text" name="address">
    <th>City</th>
    <td><input type="password" name="city" >
    <th>Pincode</th>
    <td><input type="password" name="pin">
    
    
    </tr>
    </table>
    </td>
    </table>
    <br/>
    <br/>
    
    
    <center>
    
     <input type="submit" value="SAVE" style= "width:110px; height:31px; background-image: url(pink.gif)">
    
      <input type="button" value="CANCEL" style= "width:110px; height:31px; background-image: url(pink.gif)"></a>
     
    
    </center>
    </form>
    </body>
    
    </html>
    Last edited by Markus; Apr 15 '09, 06:03 PM. Reason: Fixed code tags.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    It is normal practice to store user log-in details in a database.
    Where at least the password is encrypted.
    But this is not much use with Javascript.

    Are you asking how to do this in php?
    You will need some understanding of php to attempt this.

    Comment

    • ahilar12
      New Member
      • Feb 2009
      • 27

      #3
      reply for password and confirm password

      hi code green

      yes tell me with php itself.
      if it is easy to do it from client side(validation ) tell me in javascript.i have enclosed my database coding that is the insertion of fields of the user(insertuser .php).the previous code was the form where the user registers himself(createu ser.php).kindly check out and resolve in this very simple manner as i m a beginner for php.and the username should not be repeated.

      [code=php]

      <?php
      include 'menu.php';
      include 'connect.php';
      ?>
      <table border="0" cellpadding="0" cellspacing="0" >

      <tr>
      <td>
      <table border="0" cellpadding="0" cellspacing="0" >
      <tr>
      <td style="padding-left:20px;paddi ng-bottom:20px;">< a href="createuse r.php" style="text-decoration:none "><input type="button" name="new" value="CREATE USER" style="width:17 0px;background-image:url(pink. gif)"></a></td>
      </tr>

      <tr>
      <td style="padding-left:20px;paddi ng-bottom:20px;">< a href="users.php " style="text-decoration:none "><input type="button" name="new" value="USERS" style="width:17 0px;background-image:url(pink. gif)"></a></td>
      </tr>

      <tr>
      <td style="padding-left:20px;"><a href="reassignr ecords.php" style="text-decoration:none "> <input type="button" name="new" value="REASSIGN RECORDS" style="width:17 0px;background-image:url(pink. gif)"></a></td>
      </tr>


      </table>
      <br/>
      <br/>

      <?



      /*
      $sql="create table createuser
      (
      uid int(4) ,
      fname varchar(20),
      lname varchar(20),
      status varchar(20),
      uname varchar(20) primary key,
      pwd varchar(20),
      confirmpwd varchar(20),
      title varchar(20),
      dept varchar(20),
      phone bigint,
      address varchar(20),
      city varchar(20),
      pin bigint


      )";

      if(mysql_query( $sql,$con))
      {
      echo "table created";
      }

      if(!sql)
      {
      die(' table already exists'.mysql_e rror());
      }
      */
      $ins="insert into createuser(uid, fname,lname,sta tus,uname,pwd,c onfirmpwd,title ,dept,phone,add ress,city,pin)v alues('$_POST[uid]','$_POST[fname]','$_POST[lname]','$_POST[status]','$_POST[uname]','$_POST[pwd]','$_POST[confirmpwd]','$_POST[title]','$_POST[dept]','$_POST[phone]','$_POST[address]','$_POST[city]','$_POST[pin]')";
      $res=mysql_quer y($ins);

      /*if(!$res)
      {
      mysql_error();
      }
      */


      if((!$res)&&(my sql_errno()==10 62))
      {


      print' <b>.<font color="red" size="5">."Try another user name.This User Name already exists !!!".</font>.</b>';?>
      <a href="createuse r.php"> BACK </a>
      <?php
      // echo mysql_error();

      }
      else
      {

      echo '<h1>'. 'A new user is created successfully'.' </h1>';


      }


      ?>






      [/code]
      Last edited by Atli; Apr 15 '09, 06:51 PM. Reason: Fixed the [code] tags.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I can't write your code for you.

        But the general idea is to collect all user log in details from a HTML form.
        Then query the database to find a matching username and password.
        If the query returns a record then the login is successful.
        Otherwise report a problem to the user.

        This is pretty basic SQL/PHP but cannot be done in Javascript.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          If I'm understanding the OP correctly, he is looking for a way to match the password and the confirmation password, not to actually log the user in.
          Am I right?

          If so, all you would have to do is add a extra else if statement to your existing if statements in the JavaScript you posted, to match the "cconfirmpw d" variable to the "cpwd" variable, and return false if they don't match.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            And to compare the values, it's as simple as

            Code:
            if ( $val1 === $val2 )
            {
                // They match.
            }
            else
            {
                // The don't.
            }

            Comment

            Working...