Click to See Complete Forum and Search --> : how do I turn this off the first time a page is loaded.


TenKracer
10-28-2003, 05:43 PM
I wanted to just use a function but if I don't include the window load it gives me some wierd error about the object. but now I see the errors before the page is loaded how can I stop this?

<code>
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">

window.onload = function validate()
{
valid = "true"
if (document.diabetesform.txtHbA1c.value.length == 4)
{
var validchar = "0123456789."
for (i=0; i<4; i++)
{
if (validchar.indexOf(document.diabetesform.txtHbA1c.value.substr(i,1)))
{
if (i == 2)
{
if (document.diabetesform.txtHbA1c.value.substr(2,1) == ".")
{
valid = "true"
document.getElementById("HbA1cerrors").firstChild.nodeValue = '';
}
else
{
valid = "false"
document.getElementById("HbA1cerrors").firstChild.nodeValue = 'The HbA1c field has an invalid format. Format expected; ##.#';
}
}
}
else
{
valid = "false"
document.getElementById("HbA1cerrors").firstChild.nodeValue = 'The HbA1c field has an invalid character. Format expected; ##.#';
}
}
}
else
...
</code>

SnowCrash
10-29-2003, 07:57 AM
You get the weird object error, because you try to access an object before it is loaded (before it exists). Thats why your onload helps.

But why not calling your function when you submit your form or when the users enters or leaves a form field (document.diabetesform.txtHbA1c.onfocus / onblur) ?

Hope that helps.

TenKracer
10-29-2003, 12:45 PM
Actually it turns out it is now working the way I want it to. Another question... Is there something similar to the like operator in javascript so I could do something like... mynum LIKE "###.#"

SnowCrash
10-29-2003, 01:12 PM
Not directly, but JS supports regular expressions like in perl. I'm not the pro, but you can find tons of tutorials about reg expr on the web.

Something like

/[0-9]{3}\.[0-9]/

should do the trick
Snow Crash