JaeSun
01-29-2005, 01:49 AM
ok, trying to check a form's values... and if its wrong, change its class .. this is what i have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script>
function validate(theForm)
{
x=theForm
if (x.one.value != "a")
{
alert("You got ONE wrong")
x.one.className='wrong';
}
if (x.two.value != "b")
{
alert("You got TWO wrong")
x.two.className='wrong';
}
return false
}
</script>
<style type="text/css">
<!--
.right {color: blue;}
.wrong {color: red;}
-->
</style>
</head>
<body>
<form id="form1" onsubmit="validate(this)">
<input type="text" name="one" class="right">
<input type="text" name="two" class="right">
<input type="submit" value="Submit" />
</form>
</body>
</html>
but the problem is, everything works (changes the class and all), but then submits the document. i dont want it to submit the document. i have it returning false at the end of the function. i used this code elsewhere and it works fine when it returns false. why is it different here?
that code i based it off of and works:
function validate(theForm)
{
x=theForm
//this will check the name for only alpha characters
var charpos = x.fName.value.search("[^A-Za-z]");
if (charpos >= 0)
{
alert("You have not entered a valid name: Only Characters allowed (A-Z, a-z)")
return false
}
//this will check for the email address
at=x.email.value.indexOf("@")
dot=x.email.value.lastIndexOf(".")
//checks for @
if (at == -1)
{
alert("You have not entered a valid email address: It must be in the following format - email@domain.com")
return false
}
/*
checks to see if the "." is after the @ symbol
(to check just in case format is: myemail@whatever )
*/
if (at != -1 && dot < at)
{
alert("you have not entered a valid email address: It must be in the following format - email@domain.com")
return false
}
//this will check the format of the student ID in xxx-xx-xxxx
fdash=x.studentID.value.indexOf("-")
ldash=x.studentID.value.lastIndexOf("-")
if (fdash != 3 || ldash != 6)
{
alert("you have not entered a valid student ID: It must be in the following format - xxx-xx-xxxx")
return false
}
}
dont see why that works, but my code to check the values above doesnt work? it submits no matter what!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script>
function validate(theForm)
{
x=theForm
if (x.one.value != "a")
{
alert("You got ONE wrong")
x.one.className='wrong';
}
if (x.two.value != "b")
{
alert("You got TWO wrong")
x.two.className='wrong';
}
return false
}
</script>
<style type="text/css">
<!--
.right {color: blue;}
.wrong {color: red;}
-->
</style>
</head>
<body>
<form id="form1" onsubmit="validate(this)">
<input type="text" name="one" class="right">
<input type="text" name="two" class="right">
<input type="submit" value="Submit" />
</form>
</body>
</html>
but the problem is, everything works (changes the class and all), but then submits the document. i dont want it to submit the document. i have it returning false at the end of the function. i used this code elsewhere and it works fine when it returns false. why is it different here?
that code i based it off of and works:
function validate(theForm)
{
x=theForm
//this will check the name for only alpha characters
var charpos = x.fName.value.search("[^A-Za-z]");
if (charpos >= 0)
{
alert("You have not entered a valid name: Only Characters allowed (A-Z, a-z)")
return false
}
//this will check for the email address
at=x.email.value.indexOf("@")
dot=x.email.value.lastIndexOf(".")
//checks for @
if (at == -1)
{
alert("You have not entered a valid email address: It must be in the following format - email@domain.com")
return false
}
/*
checks to see if the "." is after the @ symbol
(to check just in case format is: myemail@whatever )
*/
if (at != -1 && dot < at)
{
alert("you have not entered a valid email address: It must be in the following format - email@domain.com")
return false
}
//this will check the format of the student ID in xxx-xx-xxxx
fdash=x.studentID.value.indexOf("-")
ldash=x.studentID.value.lastIndexOf("-")
if (fdash != 3 || ldash != 6)
{
alert("you have not entered a valid student ID: It must be in the following format - xxx-xx-xxxx")
return false
}
}
dont see why that works, but my code to check the values above doesnt work? it submits no matter what!