500 points --- Compare password & retype password field validation???

Hi,

I have a password and retype password field. I want the form to submit only when they both are same. I am already calling field level validtions on Form OnSubmit.

So please provide me the way to validate both pwd fields and from where to call the function. I think that the submit button would be a good place from where on clicking the functions is called to check both pwd fields and if not same, an alert window will appear and the form is not submitted.

Need it fast... so 500 points for this question.

Thanks,
Khurram.
LVL 2
khurram007Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mag1c1anConnect With a Mentor Commented:
So heres the solution again:

<SCRIPT LANGUAGE="JavaScript">
function CheckPassword()
{
 var Password, ConfPassword, Result;
 Password = document.frmRegister.pwd.value;
 ConfPassword = document.frmRegister.pwd2.value;
 Result = document.frmRegister.txtResult;
if(Password == ConfPassword)
return true;
else
alert ("Passwords don't match - Try again!");
return false;
}
</SCRIPT>



<form name="frmRegister">
<table>
  <tr>
     <td>
         <font face="Verdana" size="2">Password*</font></td>
     <td>
        <input type="password" class="frmText" name="pwd" /></ td>
  </tr>
 <tr>
     <td>
        <font face="Verdana" size="2">Retype Password*</font></ td>
     <td>
        <input type="password" class="frmText" name="pwd2"  /></ td>
  </tr>
 <tr>
     <td>
  <input type="submit" class="frmButton" name="Submit" value="Register" onclick="return

CheckPassword()"/>

</td>
  </tr>
<table>
</form>
0
 
mag1c1anCommented:
Hi,
Since i dont really know the contents of your html page, I am just writing the function below, it is pretty easy to understand, give me a shout if you dont understand anything or run into problems:

<SCRIPT LANGUAGE="JavaScript">
function CheckPassword()
{
 var Password, ConfPassword, Result;
 Password = document.frmRegistration.txtPassword.value;
 ConfPassword = document.frmRegistration.txtConfirmPass.value;
 Result = document.frmRegistration.txtResult;
 if(Password == ConfPassword)
   Result.value = "Congratulations - Account Created";
 else
   Result.value = "Your Passwords Do Not Match";
}
</SCRIPT>

-Mag
0
 
StormyWatersCommented:
function checkPassword(form) {
  if(form.password.value == form.retypePassword.value) {
    return true;
  } else {
    alert("Your need to type the same password in both boxes.");
    return false;
  }
}

call it in the submit button on onSubmit. To use with your other validation functions use:

onSubmit="validate() && checkPassword(this.form)"

where validate() is your field validation function.

Alternately you could just add the checkPassword code to your validation function.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mag1c1anCommented:
A few notes:
The above assumes that your form name is
"frmRegistration"
and that the first password field is
"txtPassword"
and the confirmation password field is
"txtConfirmPass"

This part you will need to change if you want an alert:
if(Password == ConfPassword)
   Result.value = "Congratulations - Account Created";
 else
   Result.value = "Your Passwords Do Not Match";



change to:
if(Password != ConfPassword)
   alert ("Congratulations - Account Created");


(Not tested, but should work)

Mag
0
 
khurram007Author Commented:
Well honestly I am not good in Javascript... so want to have something ready to cut and paste. lol

here is my html.

<table>
    <tr>
         <td>
                 <font face="Verdana" size="2">Password*</font></td>
         <td>
                <input type="password" class="frmText" name="pwd" /></td>
    </tr>
 <tr>
         <td>
               <font face="Verdana" size="2">Retype Password*</font></td>
         <td>
               <input type="password" class="frmText" name="pwd2"  /></td>
    </tr>
 <tr>
         <td>
              <input type="submit" class="frmButton" name="Submit" value="Register"/></td>
    </tr>
<table>
0
 
StormyWatersCommented:
Change your submit button code to as follows:
<input type="submit" class="frmButton" name="Submit" value="Register" onSubmit="return this.form.pwd.value == this.forn.pwd2.value"/></td>

You may have some trouble with your tables. Consider rewriting using <fieldset>s and <label>s
0
 
khurram007Author Commented:
Form name is frmRegister. I have tried this and its not working... What am I doing wrong???

<SCRIPT LANGUAGE="JavaScript">
function CheckPassword()
{
 var Password, ConfPassword, Result;
 Password = document.frmRegister.pwd.value;
 ConfPassword = document.frmRegister.pwd2.value;
 Result = document.frmRegister.txtResult;
 if(Password != ConfPassword)
   alert ("Passwords don't match - Try again!");
}
</SCRIPT>

   <input type="submit" class="frmButton" name="Submit" value="Register" onclick="CheckPassword()"/>
0
 
StormyWatersCommented:
get rid of the Result = ...; line.
0
 
khurram007Author Commented:
Ok,

Its work like a charm. But the form is still submitting. I want to stop submitting in case of alert. I have already added

return false;

but no difference.
0
 
StormyWatersCommented:
Just use this function instead, it's slightly simpler:

 function checkPassword(form) {
  if(form.pwd.value == form.pwd2.value) {
    return true;
  } else {
    alert("Your need to type the same password in both boxes.");
    return false;
  }
}

<input type="submit" class="frmButton" name="Submit" value="Register" onSubmit="return checkPassword(this.form)"/>
0
 
joke5terCommented:
oops, I am not getting a email when theres a reply here :-( will check into it and get back to you in a few mins..
0
 
joke5terCommented:
Ok, solved:


<SCRIPT LANGUAGE="JavaScript">
function CheckPassword()
{
 var Password, ConfPassword, Result;
 Password = document.frmRegister.pwd.value;
 ConfPassword = document.frmRegister.pwd2.value;
 Result = document.frmRegister.txtResult;
if(Password == ConfPassword)
return true;
else
alert ("Passwords don't match - Try again!");
return false;
}
</SCRIPT>



<form name="frmRegister">
<table>
  <tr>
     <td>
         <font face="Verdana" size="2">Password*</font></td>
     <td>
        <input type="password" class="frmText" name="pwd" /></ td>
  </tr>
 <tr>
     <td>
        <font face="Verdana" size="2">Retype Password*</font></ td>
     <td>
        <input type="password" class="frmText" name="pwd2"  /></ td>
  </tr>
 <tr>
     <td>
  <input type="submit" class="frmButton" name="Submit" value="Register" onclick="return  CheckPassword()"/>

</td>
  </tr>
<table>
</form>
0
 
joke5terCommented:
Oops, this is my old account that I used, please award the points to my new account of mag1c1an

Thanks,
Mag
0
 
StormyWatersCommented:
Again that won't work because there's no 'result' textbox anywhere. It'll throw an error.
And you should ask somewhere to have one account deleted, you're not allowed two accounts.
0
 
khurram007Author Commented:
Perfect!!!

Its working perfectly... I have a little question more... I hope you will not mind it...

I have same retype validation needed on the email fields also on the same form. I have modify the function for it. But can u tell me how to call 2 functions on a single on_click event of submit button?

I have tried this, but its not working.... Please answer here... If u want I will start another question and issue u points there.

Thanks,
Khurram.
0
 
StormyWatersCommented:
onSubmit = "return checkPassword(this.form) && checkEmail(this.form)"

...said in my original response.
0
 
khurram007Author Commented:

<input type="submit" class="frmButton" name="Submit" value="Register" onclick="return checkPassword(this.form) && checkEmail(this.form)" />

Not working!!!
0
 
khurram007Author Commented:
I don't think its good to ask another question here... So I have issued 500 points for the next one... Hope to see u all there too...

Here is the new question...

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21162192.html

Khurram.
0
 
StormyWatersCommented:
Sorry, I was assuming you were using my functions.
0
 
khurram007Author Commented:
No problem...

Cheers...

Khurram.
0
 
mag1c1anCommented:
Yeah sorry, I lost the pass to one of my accounts and my mailbox was dead so i couldnt use the reminder...anyway, long story short, a new mail box and a new id and here i am...now i got my old mailbox back..i logged in to check things out.
I dont want to delete the old account coz i have a couple of answers there in the history...deleteing that will lose that.

Cheers,
Mag
0
All Courses

From novice to tech pro — start learning today.