Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 530
  • Last Modified:

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.
0
khurram007
Asked:
khurram007
  • 8
  • 6
  • 4
  • +4
1 Solution
 
RoonaanCommented:
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-
0
 
riyasjefCommented:
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);

}

0
 
StormyWatersCommented:
Note to others: this question is a followup (actually, the same question really) to this one: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21162098.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()" />
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
KupiCommented:
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
0
 
KupiCommented:
Sorry... didn't see the other comments :|
0
 
riyasjefCommented:
..thats not ok..sorry
0
 
khurram007Author Commented:
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.
0
 
khurram007Author Commented:
sorry... not previous post...previous question... that is here...

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21162098.html
0
 
KupiCommented:
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();">
0
 
StormyWatersCommented:
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.
0
 
boblahCommented:
Hi khurram007,

to call 2 functions from the same onlick:

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

Cheers!
0
 
KupiCommented:
Ooops, I forgot:

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

becomes:

var Password, ConfPassword;
var Email, ConfEmail;

;)
0
 
StormyWatersCommented:
Sigh.
0
 
khurram007Author Commented:
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>
0
 
KupiCommented:
<!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>
0
 
StormyWatersCommented:
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()" />
0
 
mag1c1anCommented:
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>
0
 
khurram007Author Commented:
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?
0
 
KupiCommented:
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;
  }
}
0
 
KupiCommented:
Sure it's possible... but not the right way to validate a form :)
0
 
khurram007Author Commented:
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)
}
0
 
KupiCommented:
function validateForm() {
  var Name = document.frmRegister.name.value;
  var Password = document.frmRegister.pwd.value;
  var ConfPassword = document.frmRegister.pwd2.value;
  var Email = document.frmRegister.email.value;
  var ConfEmail = document.frmRegister.email2.value;
  var FullName = document.frmRegister.fullName.value;
  var FarmerNo = document.frmRegister.farmerNo.value;
  var Phone = document.frmRegister.phone1.value;
 
  if (Name == '') {
    alert ('You did not enter your name. Please provide it.');
    document.frmRegister.name.focus();
    return false;
  } else if (Password == '') {
    alert ('You did not enter your Password. Please provide it.');
    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 ('You did not enter your E-mail. Please provide it.');
    document.frmRegister.email.focus();
    return false;
  } else if (Email != ConfEmail) {
    alert ('Emails don\'t match - Try again!');
    document.frmRegister.email2.focus();
    return false;
  } else if (FullName == '') {
    alert ('You did not enter your Full Name. Please provide it.');
    document.frmRegister.fullName.focus();
    return false;
  } else if (FarmerNo == '') {
    alert ('You did not enter your Farmer No. Please provide it.');
    document.frmRegister.farmerNo.focus();
    return false;
  } else if (Phone == '') {
    alert ('You did not enter your Phone No. Please provide it.');
    document.frmRegister.phone1.focus();
    return false;
  } else {
    alert('Everything is ok!');
    return true;
  }
}
0
 
mag1c1anCommented:
My little code contribution didnt work?

Mag
0
 
khurram007Author Commented:
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...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 4
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now