...

View Full Version : HELP needed - Javascript email validation



blueoceaneyes
06-12-2008, 12:29 PM
Hi.

I have an assignment on javascript and i've done everything in the assignment except getting the email validation to work.

I have done so many searches and it seems that there's only stuff for email validation in forms.

I want to get email validation working through a prompt window so that it checks for the symbols and loops back to the start for the user to re-enter their email address should they not enter there details correctly. I have spent ages trying to fiddle with while loops etc and have spent hours and hours and it seems that it causes problems with the rest of my code not working. If anyone can provide some help that would be great. This is my last port of call.

This is a sample of my email validation

<HTML>
<HEAD>
<SCRIPT language="JavaScript">
<!--

function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit");
{
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
}
if (emailAdd.indexOf("@") == -1);
{
window.alert("You need an '@' symbol in your address, please try again.");
}
if (emailAdd.indexOf(".") == -1)
{
window.alert("You need a '.' symbol in your address, please try again.");
}
if ( (emailAdd.indexOf("@") != -1) && (emailAdd.indexOf(".") != -1) )
{
window.alert("Thanks you for your order!");
}

}
//-->
</SCRIPT>
</HEAD>
<BODY>

</BODY>

</HTML>

Pezmc
06-12-2008, 01:04 PM
<SCRIPT language="JavaScript">
<!--

function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit") {
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
} else if (emailAdd.indexOf("@") == -1) {
window.alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
window.alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
window.alert("Thanks you for your order!");
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

//-->
</SCRIPT>

Not tested but that should fix the problem as well as not bringing up more than one error message at a time. Check for excess ;'s on your if statements there were lots as well as unneeded extra brackets.

blueoceaneyes
06-13-2008, 12:26 AM
Thanks for your efforts, but this code doesn't seem to work. when i copy that code into notebook and then open as html document nothing comes up at all. Not sure why. I copied it over to see if it worked within my assignment and nothing happened either.

Thanks for trying though.

Dondon
06-13-2008, 02:46 AM
Thanks for your efforts, but this code doesn't seem to work. when i copy that code into notebook and then open as html document nothing comes up at all. Not sure why. I copied it over to see if it worked within my assignment and nothing happened either.

Thanks for trying though.

The code above should be working perfectly. Try testing it using this code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
<!--

function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit") {
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
} else if (emailAdd.indexOf("@") == -1) {
window.alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
window.alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
window.alert("Thanks you for your order!");
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

//-->
//]]>
</script>
<title></title>
</head>
<body>
<script type="text/javascript">validateEmail()</script>
</body>
</html>

rangana
06-13-2008, 03:01 AM
language is a deprecated attribute, use type instead.

alert function will suffice in replace of window.alert.

Your code should work fine, how do you want it be triggered? via onload, onclick, onfocus, onblur, onmouseover, or something else?

blueoceaneyes
06-13-2008, 04:12 AM
I want it triggered through a prompt window so that when it gets to that part in the script it will bring up a prompt window asking for email address and then test it for it's requirements.

blueoceaneyes
06-13-2008, 04:36 AM
this works really really great. thanks soo much for this. However when I try to copy over the email validation to the rest of my document script it seems to stop working and I just can't work out why. Is there anything else I need to know that could be causing it not to work when putting it into the main document. Does the function need to be up the top before the main script begins, or is it okay to have it in the sequence of when it needs to come up. Also does it create problems having more then 1 function in the document?

rangana
06-13-2008, 04:57 AM
Show us the rest of the codes, including the markups (HTML) if there are. The code you've given (should) work fine, and we can't reproduce the problem you're experiencing at our end.

blueoceaneyes
06-13-2008, 05:04 AM
here is my full code with all it's calculations etc. It's the email validation at the end that i'm having difficulty doing.

<html>
<head>
<title>
Javascript
</title>

<h1>Classroom Quotation</h1>
<script type="text/javascript">
/* <![CDATA[ */

function disp_prompt()
{
var loop=true;
while(loop)
{
var desktop=parseInt(prompt("How many desktop computers do you require for your classroom?", ""));
if (isNaN(desktop) || desktop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

var loop=true;
while(loop)
{
var laptop=parseInt(prompt ("How many laptop computers do you require for your classroom?", ""));
if (isNaN(laptop) || laptop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

if (confirm("Your order is for " + desktop + " desktop computers and " + laptop + " laptop computers?"))
{
window.alert("Your quote will calculate shorlty!");
var desktopBenches= Math.floor(desktop/4);
var laptopBenches= Math.floor(laptop/5);
var desktopRemainder =(desktop%4);
var laptopRemainder = (laptop%5);
var totalBenches = (desktopBenches + laptopBenches);
switch(desktopRemainder)
{
case 0:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 1:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 2:
if (laptopRemainder<=3)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
break;

case 3:
if (laptopRemainder<=2)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
}

var totalCable= Math.ceil(totalBenches * 200);
var totalCable= Math.ceil(totalBenches/5);

document.write("<h1>Classroom Quotation</h1>");
document.write("For " + desktop + " desktops and " + laptop + " laptops\, you will require\: \n");
document.write("<p>" + desktop + " desktops</p>");
document.write("<p>" + laptop + " laptops </p>");
document.write("<p>" + laptop + " docking stations</p>");
document.write("<p>" + totalBenches + " benches</p>");
document.write("<p>" + totalCable + " coils of network cable </p>");

if (confirm('Do you wish to accept this quote?'))
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");

if (emailAdd == "Quit")
{
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
}
else if (emailAdd.indexOf("@") == -1)
{
window.alert("You need an '@' symbol in your address, please try again.");
}
else if (emailAdd.indexOf(".") == -1)
{
window.alert("You need a '.' symbol in your address, please try again.");
}
else ((emailAdd.indexOf("@") != -1) && (emailAdd.indexOf(".") != -1))
{
window.alert("Thanks you for your order!");
}
}
}
}

/* ]]> */
</script>


<input type="button" onclick="disp_prompt()" value="Get Quote" />
</head>
<body>







</body>
</html>

rangana
06-13-2008, 05:08 AM
It's working! Which is not working? or what is suppose to do that is'nt doing (expectedly).

blueoceaneyes
06-13-2008, 05:21 AM
it looks like it's working, but it's not doing the validation properly. Like it's not checking for the correct symbols if i type it in wrong and not bringing up the prompt again like it does when it's not in this document. When i copied over the validation you had above(the one that validated properly) it wouldn't work in that code.

blueoceaneyes
06-13-2008, 05:26 AM
see this is the code with the validation that was working outside the doc. it makes the whole thing not work at all

<html>
<head>
<title>
Kristy Riddle Javascript
</title>

<h1>Classroom Quotation</h1>
<script type="text/javascript">
/* <![CDATA[ */

function disp_prompt()
{
var loop=true;
while(loop)
{
var desktop=parseInt(prompt("How many desktop computers do you require for your classroom?", ""));
if (isNaN(desktop) || desktop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

var loop=true;
while(loop)
{
var laptop=parseInt(prompt ("How many laptop computers do you require for your classroom?", ""));
if (isNaN(laptop) || laptop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

if (confirm("Your order is for " + desktop + " desktop computers and " + laptop + " laptop computers?"))
{
window.alert("Your quote will calculate shorlty!");
var desktopBenches= Math.floor(desktop/4);
var laptopBenches= Math.floor(laptop/5);
var desktopRemainder =(desktop&#37;4);
var laptopRemainder = (laptop%5);
var totalBenches = (desktopBenches + laptopBenches);
switch(desktopRemainder)
{
case 0:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 1:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 2:
if (laptopRemainder<=3)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
break;

case 3:
if (laptopRemainder<=2)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
}

var totalCable= Math.ceil(totalBenches * 200);
var totalCable= Math.ceil(totalBenches/5);

document.write("<h1>Classroom Quotation</h1>");
document.write("For " + desktop + " desktops and " + laptop + " laptops\, you will require\: \n");
document.write("<p>" + desktop + " desktops</p>");
document.write("<p>" + laptop + " laptops </p>");
document.write("<p>" + laptop + " docking stations</p>");
document.write("<p>" + totalBenches + " benches</p>");
document.write("<p>" + totalCable + " coils of network cable </p>");

if (confirm('Do you wish to accept this quote?'))
{
function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit") {
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
} else if (emailAdd.indexOf("@") == -1) {
window.alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
window.alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
window.alert("Thanks you for your order!");
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}
}
}
}

/* ]]> */
</script>

<script type="text/javascript">validateEmail()</script>
<input type="button" onclick="disp_prompt()" value="Get Quote" />
</head>
<body>







</body>
</html>

rangana
06-13-2008, 06:08 AM
Hopefully, this edition helps:


<script type="text/javascript">
/* <![CDATA[ */

function disp_prompt()
{
var loop=true;
while(loop)
{
var desktop=parseInt(prompt("How many desktop computers do you require for your classroom?", ""));
if (isNaN(desktop) || desktop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

var loop=true;
while(loop)
{
var laptop=parseInt(prompt ("How many laptop computers do you require for your classroom?", ""));
if (isNaN(laptop) || laptop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

if (confirm("Your order is for " + desktop + " desktop computers and " + laptop + " laptop computers?"))
{
window.alert("Your quote will calculate shorlty!");
var desktopBenches= Math.floor(desktop/4);
var laptopBenches= Math.floor(laptop/5);
var desktopRemainder =(desktop&#37;4);
var laptopRemainder = (laptop%5);
var totalBenches = (desktopBenches + laptopBenches);
switch(desktopRemainder)
{
case 0:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 1:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 2:
if (laptopRemainder<=3)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
break;

case 3:
if (laptopRemainder<=2)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
}

var totalCable= Math.ceil(totalBenches * 200);
var totalCable= Math.ceil(totalBenches/5);

document.write("<h1>Classroom Quotation</h1>");
document.write("For " + desktop + " desktops and " + laptop + " laptops\, you will require\: \n");
document.write("<p>" + desktop + " desktops</p>");
document.write("<p>" + laptop + " laptops </p>");
document.write("<p>" + laptop + " docking stations</p>");
document.write("<p>" + totalBenches + " benches</p>");
document.write("<p>" + totalCable + " coils of network cable </p>");

if (confirm('Do you wish to accept this quote?'))
{
validateEmail();
}
}
}
function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit") {
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
} else if (emailAdd.indexOf("@") == -1) {
window.alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
window.alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
window.alert("Thanks you for your order!");
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

/* ]]> */
</script>

blueoceaneyes
06-13-2008, 06:31 AM
for some reason when I open this in my browser, nothing happens. The screen is blank.

rangana
06-13-2008, 06:46 AM
Here's a complete one:


<html>
<head>
<title>
Kristy Riddle Javascript
</title>

<h1>Classroom Quotation</h1>
<script type="text/javascript">
/* <![CDATA[ */

function disp_prompt()
{
var loop=true;
while(loop)
{
var desktop=parseInt(prompt("How many desktop computers do you require for your classroom?", ""));
if (isNaN(desktop) || desktop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

var loop=true;
while(loop)
{
var laptop=parseInt(prompt ("How many laptop computers do you require for your classroom?", ""));
if (isNaN(laptop) || laptop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

if (confirm("Your order is for " + desktop + " desktop computers and " + laptop + " laptop computers?"))
{
window.alert("Your quote will calculate shorlty!");
var desktopBenches= Math.floor(desktop/4);
var laptopBenches= Math.floor(laptop/5);
var desktopRemainder =(desktop&#37;4);
var laptopRemainder = (laptop%5);
var totalBenches = (desktopBenches + laptopBenches);
switch(desktopRemainder)
{
case 0:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 1:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 2:
if (laptopRemainder<=3)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
break;

case 3:
if (laptopRemainder<=2)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
}

var totalCable= Math.ceil(totalBenches * 200);
var totalCable= Math.ceil(totalBenches/5);

document.write("<h1>Classroom Quotation</h1>");
document.write("For " + desktop + " desktops and " + laptop + " laptops\, you will require\: \n");
document.write("<p>" + desktop + " desktops</p>");
document.write("<p>" + laptop + " laptops </p>");
document.write("<p>" + laptop + " docking stations</p>");
document.write("<p>" + totalBenches + " benches</p>");
document.write("<p>" + totalCable + " coils of network cable </p>");

if (confirm('Do you wish to accept this quote?'))
{
validateEmail();
}
}
}
function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if(emailAdd==null)
{
alert('You\'ve choosen to cancel');
}
else if (emailAdd == "Quit") {
alert("You have chosen to Quit, refresh the screen to re-enter your order");
}
else if (emailAdd.indexOf("@") == -1) {
alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
alert("Thanks you for your order!");
}
else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

/* ]]> */
</script>

<script type="text/javascript">validateEmail()</script>
<input type="button" onclick="disp_prompt()" value="Get Quote" />
</head>
<body>
</body>
</html>

blueoceaneyes
06-13-2008, 07:06 AM
It is working now. However it is bringing up the email validation before the get quote button. I need it to do it after it's done all the calculations etc under the get quote button.

barkermn01
06-13-2008, 07:23 AM
<SCRIPT language="JavaScript">
<!--

function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if (emailAdd == "Quit") {
window.alert("You have chosen to Quit, refresh the screen to re-enter your order");
} else if (emailAdd.indexOf("@") == -1) {
window.alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
window.alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
window.alert("Thanks you for your order!");
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

//-->
</SCRIPT>

Not tested but that should fix the problem as well as not bringing up more than one error message at a time. Check for excess ;'s on your if statements there were lots as well as unneeded extra brackets.


one problem with that
} else {
window.alert("There has been a problem, please retry.");
validateEmail();
}
it is erroing you dont want it to loop coz the error is still there and it will never stop looping

rangana
06-13-2008, 07:30 AM
It is working now. However it is bringing up the email validation before the get quote button. I need it to do it after it's done all the calculations etc under the get quote button.

Because you're calling it!.

Remove the highlighted:


<html>
<head>
<title>
Kristy Riddle Javascript
</title>

<h1>Classroom Quotation</h1>
<script type="text/javascript">
/* <![CDATA[ */

function disp_prompt()
{
var loop=true;
while(loop)
{
var desktop=parseInt(prompt("How many desktop computers do you require for your classroom?", ""));
if (isNaN(desktop) || desktop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

var loop=true;
while(loop)
{
var laptop=parseInt(prompt ("How many laptop computers do you require for your classroom?", ""));
if (isNaN(laptop) || laptop < 0)
{
window.alert("Please enter a number 0 or above");
}
else
{
loop=false;
}
}

if (confirm("Your order is for " + desktop + " desktop computers and " + laptop + " laptop computers?"))
{
window.alert("Your quote will calculate shorlty!");
var desktopBenches= Math.floor(desktop/4);
var laptopBenches= Math.floor(laptop/5);
var desktopRemainder =(desktop&#37;4);
var laptopRemainder = (laptop%5);
var totalBenches = (desktopBenches + laptopBenches);
switch(desktopRemainder)
{
case 0:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 1:
if (laptopRemainder>0)
{
totalBenches++;
}
break;

case 2:
if (laptopRemainder<=3)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
break;

case 3:
if (laptopRemainder<=2)
{
totalBenches++;
}
else
{
totalBenches=totalBenches+2;
}
}

var totalCable= Math.ceil(totalBenches * 200);
var totalCable= Math.ceil(totalBenches/5);

document.write("<h1>Classroom Quotation</h1>");
document.write("For " + desktop + " desktops and " + laptop + " laptops\, you will require\: \n");
document.write("<p>" + desktop + " desktops</p>");
document.write("<p>" + laptop + " laptops </p>");
document.write("<p>" + laptop + " docking stations</p>");
document.write("<p>" + totalBenches + " benches</p>");
document.write("<p>" + totalCable + " coils of network cable </p>");

if (confirm('Do you wish to accept this quote?'))
{
validateEmail();
}
}
}
function validateEmail()
{
var emailAdd=prompt ("Enter your email addres or Quit to exit","Quit");
if(emailAdd==null)
{
alert('You\'ve choosen to cancel');
}
else if (emailAdd == "Quit") {
alert("You have chosen to Quit, refresh the screen to re-enter your order");
}
else if (emailAdd.indexOf("@") == -1) {
alert("You need an '@' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf(".") == -1) {
alert("You need a '.' symbol in your address, please try again.");
validateEmail();
} else if (emailAdd.indexOf("@") != -1 && emailAdd.indexOf(".") != -1) {
alert("Thanks you for your order!");
}
else {
window.alert("There has been a problem, please retry.");
validateEmail();
}

}

/* ]]> */
</script>

<script type="text/javascript">validateEmail()</script>
<input type="button" onclick="disp_prompt()" value="Get Quote" />
</head>
<body>
</body>
</html>

blueoceaneyes
06-13-2008, 07:38 AM
thanks so much for your help. I've got it working now! you have been sooo helpful



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum