Hi there Gurus.

I seem to have a problem in FIREFOX which does not appear in IE.

Basically, I am trying to validate a form has only 10 numbers entred into the field (no spaces or any other characters). The following script works perfectly in IE but no dice in FIREFOX. It doesn't generate any errors in Firefox, it just seems to default to false and says that the I still have to enter 10 digits where I HAVE ALREADY !

Please help.

Script as follows ...

<SCRIPT LANGUAGE="JavaScript1.2">

function digitvalidation(entered, min, max, alertbox, datatype)
{

with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(1)=="i") 
{checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}


function emptyvalidation(entered, alertbox)
{

with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}



function formvalidation(thisform)
{
with (thisform)
{

if (digitvalidation(thenumber,10,10,"You MUST enter a 10 digit number","I")==false) 
   {
    thenumber.focus(); 
    return false;
// ALWAYS SEEMS TO BE FALSE IN FIREFOX ??!?!
 } else if (!terms.checked) {
    alert("Tick the terms box.");
    return false;
 } else if (naw.value=="none") {
    alert("Select an option.");
    return false;
}

}
} 


        </script>

Well, that code looks terrible. I would try and reformat it so people can see what is going on... but it would take a while... and there isn't really any point.

Basically you are using the keyword 'with'. This keyword doesn't exist in Javascript (to my knowledge). And I'd be very surprised if it didn't give you any errors in Firefox. I suspect you just aren't aware that errors in Firefox are quietly reported in the Javascript console (Tools > JavaScript Console).

I've checked my hypothesis using the JSLint website http://www.crockford.com/javascript/jslint.html which also complains about with.

I presume that you have some knowledge of VB script... so you shouldn't have much trouble substituting this aspect of your code.

You may want to know why this works in IE... there are a couple of good reasons it works in IE (and also good reasons why you shouldn't expect it to always work)...

1. IE also supports VBscript. Where you indicated you are using Javascript, I suspect that IE is just guessing about what it is you want it to do (since what you asked for isn't what you are trying to do), the problem here is that you are allowing an ambiguous situation where the browser may or may not respond the way you intended and may perform differently on different machines.
2. IE always seems to do this, for some reason the people at Microsoft feel it is better to guess what you want it to do instead of blow up in your face... again in this situation you can never be sure how it will work.

Hi Alpha_Foobar

Thanks for the insight. The problem is now how to fix this so it works. Excuse my ignorance, I hacked this code together from a number of sources (yes, it did look better formatted but when I cut 'n paste it lost it ?!). Please tell me how you would suggest/which keyword to use instead of with ().

Thanks

Thats cool.. from what i recall the with key word is typically like an assumed pointer (in VB).

So if you take a variable that is actually a member of theForm... then it should be theMember.x instead of:

with(theForm){
whatisX = x;
}

you would write:

whatisX = theForm.x;

when you think you've got it try the JSLint link i gave you... and when you load it in firefox look at the javascript console. and don't forget to check it in IE. because working in one does not mean it will work in the other.

then if you actually want people to ALWAYS see your site.. check it in opera.... and monitor what people use... and if you find something you haven't tested, then test it.

don't take mine as an example, it only works in Firefox and partially in IE... but my site is for me (mainly).

Also, we can't stress enough the importance these days of using a correct doctype. A proper doctype will remove a large chunk of the cross-browser issues by forcing the browser to respect (to greater or lesser extent) a common DOM.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.