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.
Results 1 to 4 of 4

Thread: Alert Problem

  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Alert Problem

    Code:
    <html>
    
    <head><title>TLE</title><h1>Course Agreement</h1></head>
    
    <body>
    <tr><td><p id="name"></p></td></tr>
    <table width="300px" id="eat">
    
    
    <tr><td><b>Subjects</b></td><td><b>Units</b></td></tr>
    
    <tr><td><input type="checkbox" id="physics">Physics</td><td>3.0</td></tr>
    <tr><td><input type="checkbox" id="math">Math</td><td>3.0</td></tr>
    <tr><td><input type="checkbox" id="statistics">Statistics</td><td>3.0</td></tr>
    <tr><td><input type="checkbox" id="english">English</td><td>3.0</td></tr>
    <tr><td><button type="button" onclick="func()">Submit</button></td></tr>
    </table>
    <div id="displayp"></div>
    <div id="displaym"></div>
    <div id="displays"></div>
    <div id="displaye"></div>
    
    
    </body>
    
    <script type="text/javascript">
    var l,m,n,o,yourname,x,a,b,c,d;
    
    yourname=prompt("Please enter your name","Your name here");
    if (yourname!=null && yourname!="")
    {
    x="Welcome," + yourname ;
    document.getElementById("name").innerHTML=x;
    }
    else if (yourname==null)
    {
    a="Please enter a name";
    document.getElementById("name").innerHTML=a;
    }
    else if(yourname=="Your name here")
    {b="Please enter a name";
    document.getElementById("name").innerHTML=b;
    }
    else if(yourname=="")
    {
    c="Please enter a name";
    document.getElementById("name").innerHTML=c;
    }
    function func()
    {
    
    
    l=document.getElementById("physics")
    m=document.getElementById("math")
    n=document.getElementById("statistics")
    o=document.getElementById("english")
    
    if(l.checked==true)
    {
    document.getElementById("displayp").innerHTML="Physics";
    }
    else
    {
    document.getElementById("displayp").innerHTML=" ";
    }
    if(m.checked==true)
    {
    document.getElementById("displaym").innerHTML="Math";
    }
    else
    {
    document.getElementById("displaym").innerHTML=" ";
    }
    if(n.checked==true)	
    {
    document.getElementById("displays").innerHTML="Statistics";
    }
    else
    {
    document.getElementById("displays").innerHTML=" ";
    }
    if(o.checked==true)
    {
    document.getElementById("displaye").innerHTML="English";
    }
    else
    {
    document.getElementById("displaye").innerHTML=" ";
    }
    if(l.checked==false&&m.checked==false&&n.checked==false&&o.checked==false)
    {
    alert("Please check a box");
    return false;
    }
    
    document.getElementById("eat").innerHTML="You enrolled in the subject(s):";	
    }
    
    </script>
    
    
    </html>
    Now there's something wrong with the prompt and i just don't know what
    Last edited by cris232; 07-14-2012 at 08:58 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Code:
    if (l.checked==false&&m.checked==false&&n.checked==false&&o.checked==false) {
    alert("Please check a box");
    return false;
    }
    alerts are considered rather primitive these days - better to display a message in a <span>

    Notice unwisdom of leaving out the braces { and } in an if/else statement.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 07-14-2012 at 08:23 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It will still display "Welcome, Your name here"

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,314
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Obtaining user input via prompt boxes is completely obsolete and seen as unacceptable these days.

    If you insist (is this homework?), here is a script to prompt repeatedly until a name (or sequence of letters) is entered:-


    Code:
    <script type = "text/javascript">
    
    for (var i=1; i<2; i++) {
    var ans = prompt ("Enter your name here", "");
    if ((ans == null ) || (ans == "") || (/[^a-z\s\-\']/gi.test(ans))) {
    alert ("You must enter your name using only letters, space, hyphen and/or apostrophe!! ");
    i -- ;
    }
    }
    
    </script>
    Last edited by Philip M; 07-15-2012 at 11:36 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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