Click to See Complete Forum and Search --> : javascript help


monkeyboi
10-10-2002, 02:10 AM
i wrote a script to validate the form and 99 percent of it seem to work the way i wanted except for one littly badboy...
this if statement is giving trouble...
if (document.frmreg.agree.checked == false)
{
alert("Please read and accept our user agreement policy!");
document.frmreg.agree.focus();
return false;
}

it only work when i move it to top most of the function.... it doesn't want to be in the bottom for some reason... but i need it to be in the bottom to follow the order of the form... any idea y it does thi???

thx
here is the script
--------------------------


function validate()
{

var i = 0;



if (document.frmreg.username.value == "")
{
alert("Please pick a Username!");
document.frmreg.username.focus();
return false;
}


trim();

if (document.frmreg.username.value.length < 3)
{
alert("Your Username must be at least 3 characters!");
document.frmreg.username.focus();
return false;
}

var luser = document.frmreg.username.value.toLowerCase();

for (i =0; i <= (document.frmreg.username.value.length); i++)
{
var u = luser.charCodeAt(i);

if (u > 122 || u < 97)
{
alert("Please use letter only for your username!");
document.frmreg.username.focus();
return false;
}
}

if (document.frmreg.password.value == "")
{
alert("Please pick a Password!");
document.frmreg.password.focus();
return false;
}

if (document.frmreg.confirmpass.value == "")
{
alert("Please retype your password!");
document.frmreg.confirmpass.focus();
return false;
}

if (document.frmreg.password.value.length < 4)
{
alert("Your Password must be at least 4 characters!");
document.frmreg.password.focus();
return false;
}
if (document.frmreg.password.value != document.frmreg.confirmpass.value)
{
alert("Your Password does not Match the confirm password!");
document.frmreg.confirmpass.focus();
return false;
}

if (document.frmreg.email.value == "")
{
alert("Please enter your Email Address!");
document.frmreg.email.focus();
return false;
}



if (document.frmreg.email.value.length < 4)
{
alert("hmm doesn't seem to be a valid email address!");
document.frmreg.email.focus();
return false;
}
var txtemail = document.frmreg.email.value;
if (txtemail.indexOf('@') == -1 || txtemail.indexOf('.') == -1 || txtemail.indexOf('.') < 5
|| txtemail.indexOf('@') == txtemail.length -1 || txtemail.indexOf('@') < 3
|| txtemail.indexOf('.') < (txtemail.indexOf('@') + 2) || (txtemail.length -txtemail.indexOf('.')) > 4
|| (txtemail.length - txtemail.indexOf('.')) < 3)
{
alert("hmm doesn't seem to be a valid email address!");
document.frmreg.email.focus();
return false;
}



if (document.frmreg.city.value == "")
{
alert("Please the name of the city you live in!");
document.frmreg.city.focus();
return false;
}

if (document.frmreg.state.value == "")
{
alert("Please enter your province/state!");
document.frmreg.state.focus();
return false;
}


if (document.frmreg.year.value == "19" || document.frmreg.year.value <4)
{
alert("Please enter your year of birth!");
document.frmreg.year.focus();
return false;
}


if (document.frmreg.agree.checked == false)
{
alert("Please read and accept our user agreement policy!");
document.frmreg.agree.focus();
return false;
}

}


function trim()
{

var txtuser = document.frmreg.username.value;
var a = 0;
document.frmreg.username.value = "";

for (a = 0; a <= txtuser.length; a++)
{

if (txtuser.charAt(a) != " ")
{

document.frmreg.username.value += txtuser.charAt(a);

}


}

}