...

View Full Version : Alert Problem



cris232
07-14-2012, 08:12 PM
<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

Philip M
07-14-2012, 08:17 PM
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.

cris232
07-14-2012, 08:43 PM
It will still display "Welcome, Your name here"

Philip M
07-15-2012, 11:34 AM
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:-



<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum