Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question HELP needed - Javascript email validation

    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>

  • #2
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    <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.
    Last edited by Pezmc; 06-12-2008 at 02:10 PM. Reason: Missed the requires loopback bit

  • #3
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    this doesn't seem to work

    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.

  • #4
    New Coder
    Join Date
    Jun 2008
    Location
    127.0.0.1
    Posts
    43
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by blueoceaneyes View Post
    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:

    PHP 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("@") != -&& 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> 

  • Users who have thanked Dondon for this post:

    blueoceaneyes (06-13-2008)

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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?
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #6
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    triggered

    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.

  • #7
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #8
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #9
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    full code

    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>

  • #10
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    It's working! Which is not working? or what is suppose to do that is'nt doing (expectedly).
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #11
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #12
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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>

  • #13
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Hopefully, this edition helps:
    Code:
    <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>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #14
    New Coder
    Join Date
    Jun 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    for some reason when I open this in my browser, nothing happens. The screen is blank.

  • #15
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Here's a complete one:
    Code:
    <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>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •