Solved

500 points --- Matching Email and retype Email fields

Posted on 2004-10-09
24
505 Views
Last Modified: 2012-08-13
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
Comment
Question by:khurram007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +4
24 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 12267470
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
 
LVL 9

Expert Comment

by:riyasjef
ID: 12267484
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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12267485
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 3

Expert Comment

by:Kupi
ID: 12267492
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
 
LVL 3

Expert Comment

by:Kupi
ID: 12267499
Sorry... didn't see the other comments :|
0
 
LVL 9

Expert Comment

by:riyasjef
ID: 12267506
..thats not ok..sorry
0
 
LVL 2

Author Comment

by:khurram007
ID: 12267521
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
 
LVL 2

Author Comment

by:khurram007
ID: 12267528
sorry... not previous post...previous question... that is here...

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21162098.html
0
 
LVL 3

Expert Comment

by:Kupi
ID: 12267541
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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12267542
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
 
LVL 6

Expert Comment

by:boblah
ID: 12267549
Hi khurram007,

to call 2 functions from the same onlick:

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

Cheers!
0
 
LVL 3

Expert Comment

by:Kupi
ID: 12267550
Ooops, I forgot:

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

becomes:

var Password, ConfPassword;
var Email, ConfEmail;

;)
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12267553
Sigh.
0
 
LVL 2

Author Comment

by:khurram007
ID: 12267658
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
 
LVL 3

Expert Comment

by:Kupi
ID: 12267689
<!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
 
LVL 13

Expert Comment

by:StormyWaters
ID: 12267699
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
 
LVL 2

Expert Comment

by:mag1c1an
ID: 12267702
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
 
LVL 2

Author Comment

by:khurram007
ID: 12267707
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
 
LVL 3

Expert Comment

by:Kupi
ID: 12267735
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
 
LVL 3

Expert Comment

by:Kupi
ID: 12267741
Sure it's possible... but not the right way to validate a form :)
0
 
LVL 2

Author Comment

by:khurram007
ID: 12267804
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
 
LVL 3

Accepted Solution

by:
Kupi earned 500 total points
ID: 12267835
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
 
LVL 2

Expert Comment

by:mag1c1an
ID: 12267849
My little code contribution didnt work?

Mag
0
 
LVL 2

Author Comment

by:khurram007
ID: 12268280
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question