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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Result = document.frmRegister.txtRe sult;
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.email 2.value;
Result2 = document.frmRegister.txtRe sult2;
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.
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.v
ConfPassword = document.frmRegister.pwd2.
Result = document.frmRegister.txtRe
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
ConfEmail = document.frmRegister.email
Result2 = document.frmRegister.txtRe
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.
Also
change
function CheckEmail()
{
var Email, ConfEmail, Result2;
Email = document.frmRegister.email .value;
ConfEmail = document.frmRegister.email 2.value;
Result2 = document.frmRegister.txtRe sult2;
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.email 2.value;
Result2 = document.frmRegister.txtRe sult2;
return (Email == ConfEmail);
}
change
function CheckEmail()
{
var Email, ConfEmail, Result2;
Email = document.frmRegister.email
ConfEmail = document.frmRegister.email
Result2 = document.frmRegister.txtRe
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
ConfEmail = document.frmRegister.email
Result2 = document.frmRegister.txtRe
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.txtRe sult;
document.frmRegister.txtRe sult2;
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()" />
I said (already menitioned in the other question) that you don't have these fields in your form:
document.frmRegister.txtRe
document.frmRegister.txtRe
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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Result = document.frmRegister.txtRe sult;
Email = document.frmRegister.email .value;
ConfEmail = document.frmRegister.email 2.value;
Result2 = document.frmRegister.txtRe sult2;
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
function validateForm() {
var Password, ConfPassword, Result;
var Email, ConfEmail, Result2;
Password = document.frmRegister.pwd.v
ConfPassword = document.frmRegister.pwd2.
Result = document.frmRegister.txtRe
Email = document.frmRegister.email
ConfEmail = document.frmRegister.email
Result2 = document.frmRegister.txtRe
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
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.
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.
ASKER
sorry... not previous post...previous question... that is here...
https://www.experts-exchange.com/questions/21162098/500-points-Compare-password-retype-password-field-validation.html
https://www.experts-exchange.com/questions/21162098/500-points-Compare-password-retype-password-field-validation.html
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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Email = document.frmRegister.email .value;
ConfEmail = document.frmRegister.email 2.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();">
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.v
ConfPassword = document.frmRegister.pwd2.
Email = document.frmRegister.email
ConfEmail = document.frmRegister.email
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.
<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!
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;
;)
var Password, ConfPassword, Result;
var Email, ConfEmail, Result2;
becomes:
var Password, ConfPassword;
var Email, ConfEmail;
;)
Sigh.
ASKER
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.email 2.value;
if(Email == ConfEmail)
return true;
else
alert ("Emails don't match - Try again!");
return false;
}
</SCRIPT>
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
ConfEmail = document.frmRegister.email
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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Email = document.frmRegister.email .value;
ConfEmail = document.frmRegister.email 2.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></t d>
<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>
"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.v
ConfPassword = document.frmRegister.pwd2.
Email = document.frmRegister.email
ConfEmail = document.frmRegister.email
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><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></t
<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.email 2.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()" />
function CheckEmail() {
var Email = document.frmRegister.email
var ConfEmail = document.frmRegister.email
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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Result = document.frmRegister.txtRe sult;
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.email 2.value;
Result2 = document.frmRegister.txtRe sult2;
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>
<SCRIPT LANGUAGE="JavaScript">
function CheckPassword()
{
var Password, ConfPassword, Result;
Password = document.frmRegister.pwd.v
ConfPassword = document.frmRegister.pwd2.
Result = document.frmRegister.txtRe
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
ConfEmail = document.frmRegister.email
Result2 = document.frmRegister.txtRe
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>
<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>
<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>
ASKER
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.v alue;
ConfPassword = document.frmRegister.pwd2. value;
Email = document.frmRegister.email .value;
ConfEmail = document.frmRegister.email 2.value;
if (Password == '') {
alert ('Passwords is required!');
document.frmRegister.pwd.f ocus();
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.email 2.focus();
return false;
} else {
alert('Everything is ok!');
return true;
}
}
function validateForm() {
var Password, ConfPassword;
var Email, ConfEmail;
Password = document.frmRegister.pwd.v
ConfPassword = document.frmRegister.pwd2.
Email = document.frmRegister.email
ConfEmail = document.frmRegister.email
if (Password == '') {
alert ('Passwords is required!');
document.frmRegister.pwd.f
return false;
} else if (Password != ConfPassword) {
alert ('Passwords don\'t match - Try again!');
document.frmRegister.pwd2.
return false;
} else if (Email == '') {
alert ('E-mail is required!');
document.frmRegister.email
return false;
} else if (Email != ConfEmail) {
alert ('Emails don\'t match - Try again!');
document.frmRegister.email
return false;
} else {
alert('Everything is ok!');
return true;
}
}
Sure it's possible... but not the right way to validate a form :)
ASKER
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:
-------------------------- ---------- ---------- ---------- --
-------------------------S tart------ ---------- ---------- -
-------------------------- ---------- ---------- ---------- --
function checkme() //check for required fields
{
if (document.forms[0].name.va lue == "")
{alert("You did not enter your name. Please provide it.");
document.forms[0].name.foc us();retur n(false)
}
if (document.forms[0].pwd.val ue == "")
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd.focu s();return (false)
}
if (document.forms[0].pwd2.va lue == "")
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd2.foc us();retur n(false)
}
if (document.forms[0].email.v alue == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email.fo cus();retu rn(false)
}
if (document.forms[0].email2. value == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email2.f ocus();ret urn(false)
}
if (document.forms[0].fullNam e.value == "")
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].fullName .focus();r eturn(fals e)
}
if (document.forms[0].farmerN o.value == "")
{alert("You did not enter your Farmer No. Please provide it.");
document.forms[0].farmerNo .focus();r eturn(fals e)
}
if (document.forms[0].phone1. value == "")
{alert("You did not enter your Phone No. Please provide it.");
document.forms[0].phone1.f ocus();ret urn(false)
}
validated(this)
}
This is the script i am using:
--------------------------
-------------------------S
--------------------------
function checkme() //check for required fields
{
if (document.forms[0].name.va
{alert("You did not enter your name. Please provide it.");
document.forms[0].name.foc
}
if (document.forms[0].pwd.val
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd.focu
}
if (document.forms[0].pwd2.va
{alert("You did not enter your Password. Please provide it.");
document.forms[0].pwd2.foc
}
if (document.forms[0].email.v
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email.fo
}
if (document.forms[0].email2.
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].email2.f
}
if (document.forms[0].fullNam
{alert("You did not enter your Full Name. Please provide it.");
document.forms[0].fullName
}
if (document.forms[0].farmerN
{alert("You did not enter your Farmer No. Please provide it.");
document.forms[0].farmerNo
}
if (document.forms[0].phone1.
{alert("You did not enter your Phone No. Please provide it.");
document.forms[0].phone1.f
}
validated(this)
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My little code contribution didnt work?
Mag
Mag
ASKER
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...
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...
<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-