Link to home
Start Free TrialLog in
Avatar of khurram007
khurram007

asked on

500 points --- Matching Email and retype Email fields

Hi,

The first function for Matching passwords is working perfectly... the second one for email is not.... Here is the code for both...

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


function CheckEmail()
{
 var Email, ConfEmail, Result2;
 Email = document.frmRegister.email.value;
 ConfEmail = document.frmRegister.email2.value;
 Result2 = document.frmRegister.txtResult2;
if(Email == ConfEmail)
return true;
else
alert ("Emails don't match - Try again!");
return false;
}
</SCRIPT>


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?

Thanks,
Khurram.
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Mjah:

<script type="text/javascript">
function multi_function()
{
 if(!CheckPassword()) return false;
 if(!CheckMail()) return false;
 return true;
}
</script>
<form onsubmit="return multi_function();">

Regards

-r-
Avatar of riyasjef
riyasjef

Also

change

function CheckEmail()
{
 var Email, ConfEmail, Result2;
 Email = document.frmRegister.email.value;
 ConfEmail = document.frmRegister.email2.value;
 Result2 = document.frmRegister.txtResult2;
if(Email == ConfEmail)
return true;
else
alert ("Emails don't match - Try again!");
return false;
}

to

function CheckEmail()
{
 var Email, ConfEmail, Result2;
 Email = document.frmRegister.email.value;
 ConfEmail = document.frmRegister.email2.value;
 Result2 = document.frmRegister.txtResult2;
   return (Email == ConfEmail);

}

Note to others: this question is a followup (actually, the same question really) to this one: https://www.experts-exchange.com/questions/21162098/500-points-Compare-password-retype-password-field-validation.html

I said (already menitioned in the other question) that you don't have these fields in your form:
document.frmRegister.txtResult;
document.frmRegister.txtResult2;

Thus, referencing them in your script won't work. Already mentioned.

Finally, you can (already mentioned in the other question) call both functions with this:
<input type="submit" class="frmButton" name="Submit" value="Register" onclick="return CheckPassword() && CheckEmail()" />
You should call a function at the onsubmit event of the form and do all your validations in it, i.e.:

function validateForm() {
  var Password, ConfPassword, Result;
  var Email, ConfEmail, Result2;
 
  Password = document.frmRegister.pwd.value;
  ConfPassword = document.frmRegister.pwd2.value;
  Result = document.frmRegister.txtResult;

  Email = document.frmRegister.email.value;
  ConfEmail = document.frmRegister.email2.value;
  Result2 = document.frmRegister.txtResult2;
 
  if (Password != ConfPassword) {
    alert ('Passwords don't match - Try again!');
    return false;
  } elseif (Email != ConfEmail) {
    alert ('Emails don't match - Try again!');
    return false;
  } else {
    alert('Everything is ok!');
    return true;
  }
}

<form onsubmit="return validateForm();">

Kupi
Sorry... didn't see the other comments :|
..thats not ok..sorry
Avatar of khurram007

ASKER

I don't think there is a problem in posting this new question. Every one can see that I already have assigned points there. And as I was not thinking it justification to ask another question after so many posts, i issued this question.

Is there any problem with it?

Also I already have mentioned in my previous post that the solution provided here by StormyWaters is not working for me.
khurram007,
i searched your code for txtResult1 and txtResult2 fields but didn't find them... are you sure you need them? If not, my code becomes:

function validateForm() {
  var Password, ConfPassword, Result;
  var Email, ConfEmail, Result2;
 
  Password = document.frmRegister.pwd.value;
  ConfPassword = document.frmRegister.pwd2.value;

  Email = document.frmRegister.email.value;
  ConfEmail = document.frmRegister.email2.value;
 
  if (Password != ConfPassword) {
    alert ('Passwords don't match - Try again!');
    return false;
  } elseif (Email != ConfEmail) {
    alert ('Emails don't match - Try again!');
    return false;
  } else {
    alert('Everything is ok!');
    return true;
  }
}

Just one function in which you can add more and more validations.

P.S. remember to call it in the form onsubmit:

<form onsubmit="return validateForm();">
Sorry, I was assuming you were using my functions, not his. You should be more clear about who's post you're talking about. though I understand that English isn't everyone's native language. Change the function names, then:

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

Or, as Kupi said, combine the functions.
Hi khurram007,

to call 2 functions from the same onlick:

onclick="return (CheckPassword() && CheckEmail());"

Cheers!
Ooops, I forgot:

var Password, ConfPassword, Result;
var Email, ConfEmail, Result2;

becomes:

var Password, ConfPassword;
var Email, ConfEmail;

;)
Well,

I still am not able to make it work...

Then I tried to check check Email function but its not working although on onclick of submit button I changed it from checkpassword() to checkemail to work in single. But still not working... Seems like problem in function. Here is the code I am using(after StormyWaters correction)

<SCRIPT LANGUAGE="JavaScript">
function Checkemail()
{
 var Email, ConfEmail;
 Email = document.frmRegister.email.value;
 ConfEmail = document.frmRegister.email2.value;
if(Email == ConfEmail)
return true;
else
alert ("Emails don't match - Try again!");
return false;
}
</SCRIPT>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title></title>

<script type="text/javascript">

function validateForm() {
  var Password, ConfPassword;
  var Email, ConfEmail;
 
  Password = document.frmRegister.pwd.value;
  ConfPassword = document.frmRegister.pwd2.value;

  Email = document.frmRegister.email.value;
  ConfEmail = document.frmRegister.email2.value;
 
  if (Password != ConfPassword) {
    alert ('Passwords don\'t match - Try again!');
    return false;
  } else if (Email != ConfEmail) {
    alert ('Emails don\'t match - Try again!');
    return false;
  } else {
    alert('Everything is ok!');
    return true;
  }
}

</script>

</head>

<body>

<form name="frmRegister" onsubmit="return validateForm();">

<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><font face="Verdana" size="2">E-mail*</font></td>
    <td><input type="password" class="frmText" name="email" /></td>
  </tr>
  <tr>
    <td><font face="Verdana" size="2">Retype E-mail*</font></td>
    <td><input type="password" class="frmText" name="email2" /></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" class="frmButton" name="Submit" value="Register" /></td>
  </tr>
<table>

</form>

</body>

</html>
Oh, I see the problem. Since you have multiple statements in the 'if' you need to enclose them with curly brackets like follows:

function CheckEmail() {
  var Email = document.frmRegister.email.value;
  var ConfEmail = document.frmRegister.email2.value;
  if(Email == ConfEmail) {
    return true;
  } else {
    alert("Emails don't match - Try again!");
    return false;
  }
}

otherwise you may get unexpected results.

And, you should be using the onSubmit handler rather than the onClick handler. My mistake on that part, sorry for the confusion.

to summarize,
use that function above, plus the similar password function, used in conjunction with the following tag. Make sure that you've got the right capitalization for function names as well, as javascript is case-sensitive.
<input type="submit" class="frmButton" name="Submit" value="Register" onsubmit="return CheckPassword() && CheckEmail()" />
Heres the solution: (sorry, was watching a movie :-)   )

<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;
CheckEmail()
else
alert ("Passwords don't match - Try again!");
return false;
}


function CheckEmail()
{
 var Email, ConfEmail, Result2;
 Email = document.frmRegister.email.value;
 ConfEmail = document.frmRegister.email2.value;
 Result2 = document.frmRegister.txtResult2;
if(Email == ConfEmail)
return true;
else
alert ("Emails 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>

<tr>
     <td>
         <font face="Verdana" size="2">Password*</font></td>
     <td>
        <input type="text" class="frmText" name="email" /></ td>
  </tr>
 <tr>
     <td>
        <font face="Verdana" size="2">Retype Password*</font></ td>
     <td>
        <input type="text" class="frmText" name="email2"  /></ td>
  </tr>
 <tr>
     <td>
  <input type="submit" class="frmButton" name="Submit" value="Register" onclick="return

CheckPassword()"/>

</td>
  </tr>
<table>
</form>
I don't want to use in Form OnSubmit cauz I have already other function there. I want it to use in Sumit Button Onclick Event. I hope it is possible... right?
I suppose you need password and e-mail fileds not to be empty... in this case, overwrite the script with this (also sets focus on empty fields):

function validateForm() {
  var Password, ConfPassword;
  var Email, ConfEmail;
 
  Password = document.frmRegister.pwd.value;
  ConfPassword = document.frmRegister.pwd2.value;

  Email = document.frmRegister.email.value;
  ConfEmail = document.frmRegister.email2.value;
 
  if (Password == '') {
    alert ('Passwords is required!');
    document.frmRegister.pwd.focus();
    return false;
  } else if (Password != ConfPassword) {
    alert ('Passwords don\'t match - Try again!');
    document.frmRegister.pwd2.focus();
    return false;
  } else if (Email == '') {
    alert ('E-mail is required!');
    document.frmRegister.email.focus();
    return false;
  } else if (Email != ConfEmail) {
    alert ('Emails don\'t match - Try again!');
    document.frmRegister.email2.focus();
    return false;
  } else {
    alert('Everything is ok!');
    return true;
  }
}
Sure it's possible... but not the right way to validate a form :)
Ok Kupi... you got the point... I have other Javascript function checking the empty values... I hope that you or some one can incorporate it and the password & retype password, and email & retype email functions all into one...

This is the script i am using:

----------------------------------------------------------
-------------------------Start---------------------------
----------------------------------------------------------
function checkme() //check for required fields
{
if (document.forms[0].name.value == "")
{alert("You did not enter your name. Please provide it.");
document.forms[0].name.focus();return(false)
}

if (document.forms[0].pwd.value == "")
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd.focus();return(false)
}

if (document.forms[0].pwd2.value == "")
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd2.focus();return(false)
}

if (document.forms[0].email.value == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email.focus();return(false)
}

if (document.forms[0].email2.value == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email2.focus();return(false)
}

if (document.forms[0].fullName.value == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].fullName.focus();return(false)
}

if (document.forms[0].farmerNo.value == "")
{alert("You did not enter your Farmer No. Please provide it.");
document.forms[0].farmerNo.focus();return(false)
}

if (document.forms[0].phone1.value == "")
{alert("You did not enter your Phone No. Please provide it.");
document.forms[0].phone1.focus();return(false)
}
validated(this)
}
ASKER CERTIFIED SOLUTION
Avatar of Kupi
Kupi

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
My little code contribution didnt work?

Mag
Mag,

Your code worked really fine... but when I tried to use another function based on yours, it wasn't working... And i finally found Kupi's solution better as all the functions are incorporated as one now... and working perfectly...

Thanks Kupi... You got the points...