Click to See Complete Forum and Search --> : Basic Validation Script
spacehog
02-28-2003, 06:23 PM
I just copy/pasted the "Basic Validation" script from this site and was able to make fields on my forms be required.
What I would like to do is have a field be required depending on the status of another field. I have 3 sets of 2 drop-down boxes. Within each drop-down box pair the second box is required only if the value of the first drop-down box is 2 or 3. If the value of the first drop-down box is 1 then the second drop-down box is not required.
Does anyone know if a script like this exists? Or how I could modify the Basic Validation script to add this?
AdamBrill
02-28-2003, 10:47 PM
Take a look at this:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script language=javascript>
function validate()
{
if(form1.D1.value==2 || form1.D1.value==3){
if(form1.D2.value!="select"){
alert(form1.D2.value);
return true;
}
}
else if(form1.D1.value=="select"){
return false;
}else{
return true;
}
return false;
}
</script>
</head>
<body>
<form method="POST" name=form1 onsubmit="return validate()" action="whatever.php">
<select size="1" name="D1">
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><select size="1" name="D2">
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</form>
</body>
</html>
Does that help?
spacehog
03-03-2003, 05:11 PM
That script help shed some light on JavaScript, but I could not get it to validate whether the form.D2 = "select". I even replaced the alert with a text message and I could not get the form to give me an error.
AdamBrill
03-03-2003, 05:15 PM
Well, you changed form.D2.value to form.D2. If you want to check if it is select, then try this:
if(form1.D2.value=="select"){
//do whatever
}
I hope that helps. :)
spacehog
03-04-2003, 09:06 AM
Maybe I am doing something wrong. Below is the script I am using. From my code, I thought if the first drop-down had a value of 2 or 3. And the second drop-down had a value of "select", I would get a message that said "Hi".
Instead , no matter what combination (in the drop-downs) I try the values are passed to the action page. I wanted to see a case where they failed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<script language=javascript>
function validate()
{
if(form1.D1.value==2 || form1.D1.value==3){
if(form1.D2.value=="select"){
alert("Hi");
return true;
}
}
else if(form1.D1.value=="select"){
return false;
}else{
return true;
}
return false;
}
</script>
</head>
<body>
<form action="testvaldump.cfm" method="post" name="form1" id="form1" onSubmit="return validate()">
<select size="1" name="D1">
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><select size="1" name="D2">
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</form>
</body>
</html>
cyberade
03-04-2003, 10:35 AM
I don't know if this will help at all but I think you're problem is to do with mixing strings and numbers.
I changed all occurrences of "select" to a 0 (zero) and the alert popped up.
HTH.
AdamBrill
03-04-2003, 12:44 PM
umm... Try this instead:
<html>
<head>
<title>Untitled</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.form1.D1.options[document.form1.D1.selectedIndex].value==2 || document.form1.D1.options[document.form1.D1.selectedIndex].value==3){
if(document.form1.D2.options[document.form1.D2.selectedIndex].value=="select"){
alert("Hi");
return true;
}
}
else if(document.form1.D1.options[document.form1.D1.selectedIndex].value=="select"){
return false;
}else{
return true;
}
return false;
}
function pulldown() {
formobj = documentform1.D1;
var alerts = formobj.options[formobj.selectedIndex].value;
if (alerts == 1)
{
alert("Alert One");
}
if (alerts == 2)
{
alert ("Alert Two");
}
return false;
}
</script>
</head>
<body>
<form action="http://www.yahoo.com" method="post" name=form1 onsubmit="return validate();">
<select size="1" name=D1>
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><select size="1" name="D2">
<option value="select">Please Select</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
</select>
<br><input type="Submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2">
</form>
</body>
</html>
I forgot to make it cross-browser. :) oops...