Link to home
Start Free TrialLog in
Avatar of khurram007
khurram007

asked on

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.
Avatar of mag1c1an
mag1c1an

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
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.
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
Avatar of khurram007

ASKER

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>
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
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()"/>
get rid of the Result = ...; line.
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.
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)"/>
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..
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>
Oops, this is my old account that I used, please award the points to my new account of mag1c1an

Thanks,
Mag
ASKER CERTIFIED SOLUTION
Avatar of mag1c1an
mag1c1an

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
onSubmit = "return checkPassword(this.form) && checkEmail(this.form)"

...said in my original response.

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

Not working!!!
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...

https://www.experts-exchange.com/questions/21162192/500-points-Matching-Email-and-retype-Email-fields.html

Khurram.
Sorry, I was assuming you were using my functions.
No problem...

Cheers...

Khurram.
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