...

View Full Version : onclick value of field check



Crash1hd
06-13-2003, 09:34 AM
The Following code for somereason doesnt work in netscape 4.7 works great in all other browsers?


function trimtext(txt){
var temptext=txt.replace(/\s*/,'');
ets = temptext.substring(temptext.length-1, temptext.length);
while(ets==" "){
temptext = temptext.substring(0, temptext.length-1);
ets = temptext.substring(temptext.length-1, temptext.length);
}
return temptext;
}

function CheckObits(x)
{
var tempstr = trimtext(x.value);
var temparray = tempstr.split(/\s+/);
var arylen = temparray.length;

var over50 = arylen-50;
var over100 = arylen-100;
var over250 = arylen-250;
var over350 = arylen-350;
var Obit = document.Form1;
var D = new Date();
var Year = D.getFullYear();

if (Obit.PurchaseField.value == "50")
{
if (arylen >= "51")
{
if (arylen <= "100")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 100 word Obituary!");
return
}
}
if (arylen >= "101")
{
if (arylen <= "250")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 250 word Obituary!");
return
}
}
if (arylen >= "251")
{
if (arylen <= "350")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 350 word Obituary!");
return
}
}
if (arylen >= "351")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose Unlimited word Obituary!");
return
}
}
Obit.submit()
}

I know that its check the field but the alerts are not being displayed (the reason I know that it is checking is because If i remove Obit.submit() then when I click on submit it wont change the page

here is what is in the htm page


<input type="Button" value="Submit" onclick="CheckObits(DetailsField);">&nbsp;

requestcode
06-13-2003, 01:13 PM
I think for NS4 version browsers you need to chang your function call to this:
<input type="Button" value="Submit" onclick="CheckObits(document.form1.DetailsField.value);">

Then change this line in your function:
var tempstr = trimtext(x);

Crash1hd
06-13-2003, 08:30 PM
Ok I did the changes that you had indicated but it still does the same thing as before which is if I have text in the box more then 50 words it still lets it through

Ok I noticed that when I run the code above it works fine in netscape but the problem is when I have more then one I relized that after

so the problem is with this code


if (Obit.PurchaseField.value = "50")
{
if (arylen >= "51")
{
if (arylen <= "100")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 100 word Obituary!");
return
}
}
if (arylen >= "101")
{
if (arylen <= "250")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 250 word Obituary!");
return
}
}
if (arylen >= "251")
{
if (arylen <= "350")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose 350 word Obituary!");
return
}
}
if (arylen >= "351")
{
alert("You are " + over50 + " Word\(s\) Over!\nPlease shorten Obituary or choose Unlimited word Obituary!");
return
}
}
if (Obit.PurchaseField.value = "100")
{
if (arylen <= "50")
{
alert("You are useing less then 50 Words!\nPlease add to Obituary or choose 50 word Obituary! Which is free \:\)");
return
}
if (arylen >= "101")
{
if (temparray.length <= "250")
{
alert("You are " + over100 + " Word\(s\) Over!\nPlease shorten Obituary or choose 250 word Obituary!");
return
}
}
if (arylen >= "251")
{
if (temparray.length <= "350")
{
alert("You are " + over100 + " Word\(s\) Over!\nPlease shorten Obituary or choose 350 word Obituary!");
return
}
}
if (arylen >= "351")
{
alert("You are " + over100 + " Word\(s\) Over!\nPlease shorten Obituary or choose Unlimited word Obituary!");
return
}
}
Obit.submit()
}


Also tried else if doesnt work

so what I want to do is I have a drop down that has choices of 50 100 250 ext... and a comment box that the value has to be less then or equal to the number in the drop down

glenngv
06-16-2003, 03:38 AM
are these just typos in your post?

if (Obit.PurchaseField.value = "50")

...

if (Obit.PurchaseField.value = "100")


you should use == instead.

Crash1hd
06-16-2003, 06:53 AM
When I use == which I had originally then netscape doesnt do what I want it to do at all but if I use just = and have just the one choice it works!

glenngv
06-16-2003, 07:04 AM
if you do it that way, you are not checking for a condition but just assigning that field the value, and all the succeeding statements will be executed.

if (Obit.PurchaseField.value = "50")//assigns purchase field to 50
{
//statements here will be executed
}

if (Obit.PurchaseField.value = "100")//assigns purchase field to 100
{
//statements here will ALSO be executed
}


Is that the way it is supposed to work?

Crash1hd
06-16-2003, 07:07 AM
no its just supposed to check the value then if the condition is false then excute the alerts

glenngv
06-16-2003, 07:50 AM
That's exactly what I was pointing out to you. Use == not =.

Crash1hd
06-16-2003, 07:52 AM
Ok cool but when I use == netscape doesnt do what I want it to do?

glenngv
06-16-2003, 07:57 AM
post the whole code and I can test it for you. I have NS4.78 in my machine.

Crash1hd
06-16-2003, 08:11 AM
This is the 2 files that are used! Hope that works :)

glenngv
06-16-2003, 09:10 AM
I haven't run the page because it is an asp page.

But I noticed, as requestcode pointed out earlier, you should change the function call to:

<input type="Button" value="Submit" onclick="CheckObits(this.form.DetailsField);">

or

<input type="Button" value="Submit" onclick="CheckObits(document.forms[0].DetailsField);">


to reference a field PROPERLY, you should follow this syntax:

document.formNameHere.elementNameHere

IE allows referencing fields by just the name but this is not the proper way. You should make it a habit to do it the correct way.

Try also naming your form.

Crash1hd
06-16-2003, 09:13 AM
yep tried that unfortunatly that didnt fix the problem and for some reason I have to name the form as Form1 due to another Javascript where I tried to change it but the javascript faled to run so I was not really woried about it!

glenngv
06-16-2003, 09:33 AM
remove the quotes in all the arylen conditions like:

if (arylen >= 51)

and try alerting at the start of the function

function CheckObits(x)
{
alert(x);//should alert object
...

you can easily debug if you can find the error message displayed by NS4. type javascript&#58; in the address bar to open the Javascript console.

Crash1hd
06-16-2003, 07:54 PM
Ok but it doesnt cause an error it just ignores it like there was nothing to check?

Owl
06-17-2003, 12:42 AM
Hi Crash1hd,

Change every occurance of:

Obit.PurchaseField.value

to:

Obit.PurchaseField[Obit.PurchaseField.selectedIndex].value

( ) ( )
>>V

glenngv
06-17-2003, 02:44 AM
I didn't notice that PurchaseField is a <select> field.

Obit.PurchaseField.options[Obit.PurchaseField.selectedIndex].value

I don't know if Owl's code is the same as mine. But I think you need the .options in there.

Crash1hd
06-17-2003, 03:41 AM
Well That made it work thanks all :)

Now If I could just figure out why it doesnt work with Opera and I would be set?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum