Click to See Complete Forum and Search --> : need help with JS


xdef
04-22-2003, 01:21 PM
The following JS works, but I can't get the checkbox part to work.

Can someone find the answer to this?

thanks...xdef






<script language="JavaScript">

<!--

function isMailReady(form) {

var passed = true;
fieldtofocus = "";




message ="Please enter one of the following required fields: \n"


if (form.person.value == "") {
message += "Your Name \n";
passed = false;

fieldtofocus = form.person;
}

if (form.company.value == "") {
message += "your company /n";
passed = true;

fieldtofocus = form.company;
}

if (form.address1.value == "") {
message += "Address \n";
passed = true;

if (fieldtofocus == "") {fieldtofocus = form.address1}
}

if (form.town.value == "") {
message += "Your Town \n";
passed = false;

if (fieldtofocus == "") {fieldtofocus = form.town}
}
if (form.state.value == "") {
message += " State \n";
passed = false;
if (fieldtofocus == "") {fieldtofocus = form.state}
}
if (form.postcode.value == "") {
message += "Postcode \n";
passed = false;
if (fieldtofocus == "") {fieldtofocus = form.postcode}
}
if (form.email.value == "") {
message += "Email \n";
passed = false;
if (fieldtofocus == "") {fieldtofocus = form.email}
}

if (form.quote.value =="") {
message += "Please choose a checkbox \n";
passed = false;
}

if (passed == false) {
fixFieldInfo(message, fieldtofocus);
}

return passed;
}





function fixFieldInfo(message, fieldtofocus) {
alert(message);
fieldtofocus.focus();
}


// -->
</script>
</head>

Jona
04-22-2003, 01:24 PM
Where are your checkboxes? You didn't post any HTML... Besides, I'd rather you just post the part that you're having trouble with, rather than the whole code. ;)

havik
04-22-2003, 03:20 PM
I'm assuming that this is your code to check if the checkboxes have been checked? Well, this is the incorrect way to do so:

if (form.quote.value =="") {
message += "Please choose a checkbox \n";
passed = false;
}

Here is one way to do it, but this way will only work if the checkboxes have the same name:

boxchecked = false;

for(i = 0; i < form.quote.length; i++) {
if(form.quote[i].checked == true) {
boxchecked = true;
}
}
if(boxchecked == false){
message += "Please choose a checkbox \n";
passed = false;
}

Havik

xdef
04-22-2003, 10:32 PM
.......just post the part you are having trouble with.....

I did. It is the javascript part. Why would you need to see the html? It is just a checkbox or more than one. What does it matter. I am after the proper syntax that's all.

Jona
04-22-2003, 10:53 PM
I know that. I wasn't going to search through all of your code, though. If you wanted me to help, I would've, if you would've posted exclusively the checkbox part.

xdef
04-22-2003, 11:01 PM
This is getting kinda frustrating.

Are you who replied js programmers or not? Seems to be a lot of whinging and not much action!

onto.....HAVIK....... that code for the checkbox didn't work. You say my code will not work yet offer no explanation. So why won't it work.? It looks logical to me. I don't think your code fits into the js seen here.

Did you test your script? Obviously not cause it doesn't work.

Jona
04-22-2003, 11:05 PM
I am just telling you that next time you post, try to get down to specific lines of code, rather than posting the whole thing. You know I would've answered better the first time if you would have been more specific in the code you needed help with. Go ahead, Havik, work with him. I give up. :mad:

xdef
04-22-2003, 11:23 PM
Jona, you are being difficult and besides that you have offered nothing constructive towards fixing this problem. You should be glad I'm not looking to pay someone to write my js cause I'd still be looking.

You cannot post a little part of js. It's all part of itself.

Boy. What a difficult person. I'm outa here. Find myself a real programmer.

khaki
04-22-2003, 11:34 PM
hi xdef...

You are the one who requested help.

I think that it is rude of you to criticize those who took their time to try to help you (regardless of your opinion of the result of that help).

I personally know Havik and Jona to be very generous and knowledgable in thier assistance with helping people in this forum.

All I can really say at this point is... that as someone who is looking for help, you show a strange attitude towards attracting it.

:rolleyes: k

pyro
04-23-2003, 08:16 AM
You don't seem very appreciative of those trying to help you. Remember that no one gets paid to answer questions on this board.

Anyway, this code should be enough to get you going. You will have to substitue your values, etc, but it provides the syntax.

<html>
<head>
<script language="javascript" type="text/javascript">
function test(frm)
{
isChecked=0;
for (i=0; i < frm.mycheckbox.length; i++)
{
if (document.myform.mycheckbox[i].checked == true)
{
isChecked=1;
}
}
if (isChecked == 1)
{
alert ("A checkbox was selected");
}
else
{
alert ("A checkbox was not selected");
}
}

</script>
</head>
<body>
<form name="myform" onsubmit="test(this)">
yes<input name="mycheckbox" type="checkbox" value="yes">
no<input name="mycheckbox" type="checkbox" value="no">
<input type="submit" value="submit">
</form>
</body>
</html>

havik
04-23-2003, 08:47 AM
Remember, I said I was assuming that you used form.quote.value to validate the checkboxes because you didn't specify which code exactly controlled that aspect. If that's not the case then please be more specific next time.

Your code didn't work because this:
if (form.quote.value =="")
isn't the proper way to check if a checkbox is checked or not

explanation of my code:
Will only work if the checkboxes have the same name
example, they all start with this:
<input type=checkbox name=quote .. >

// set to false because not one box is known to be checked
boxchecked = false;

// loop from 0 to number of checkboxes
for(i = 0; i < form.quote.length; i++) {

// if checkbox i checked is equal to true
if(form.quote[i].checked == true) {

// then this is true because at least one box is checked
boxchecked = true;

}
}

// if not one box is checked, then boxchecked will come out
// false, otherwise, it'd be true
if(boxchecked == false){

// if it is false then "please choose a checkbox
message += "Please choose a checkbox \n";
passed = false;
}

Havik

xdef
04-23-2003, 10:45 AM
Yes, but the problem I have is that the four checkboxes must have unique names so that the client knows which one has been checked.

This must alter your code above. Correct? I was trying to do a test with the code existing to see if "quote" was checked. But obviously I cant do this. The code needs to verify that at least one (well only one) of the uniquely named boxes is checked.

pyro
04-23-2003, 10:50 AM
The code that I posted can be modified to do that without too much trouble. If you don't know how to do this, post the code you are using (including HTML)...

havik
04-23-2003, 10:50 AM
If the names are unique and there are only four then this way will be easy:

if(form.name1.checked == true || form.name2.checked == true || form.name3.checked == true || form.name4.checked == true){
// then one of these has been checked and passed is equal to true
}
else {
// none are true and passed is equal to false
}

Not the prettiest way but it works.

Havik

xdef
04-23-2003, 10:53 AM
script attached

had to save this as txt file.

pyro
04-23-2003, 11:44 AM
Ok, I stripped your code down to make it easier for me...you can add it back in.

function isMailReady(form) {
isChecked = 0;
if (form.quote.checked == true)
{
isChecked++;
}
if (form.quote_repair.checked == true)
{
isChecked++;
}
if (form.maintenance_quote.checked == true)
{
isChecked++;
}
if (form.consultancy.checked == true)
{
isChecked++;
}
if (isChecked == 0)
{
alert ("You must select a checkbox \n");
return false;
}
if (isChecked > 1)
{
alert("You may only select one checkbox \n");
return false;
}
return true;
}

xdef
04-23-2003, 01:55 PM
doesn't work.

I can see the code looks right, as did every other code I've tried so far. Did you test this?

IE5/Mac0s9+

xdef
04-23-2003, 02:00 PM
I should say....the code works on its own, and the original code for other areas also works on its own, but the two together cancels out the checkboxes. I'll go through it with an alert message to try to find out where it faults.