I use the following code to make sure a field is filled in:
<script>
if (Form1.Name.value == "")
{
alert("Please enter a value for the FIRST NAME field.");
Form1.Name.focus();
return (false);
}
</script>
What I am needing is a field to be required if another fields value
equals "Yes"
Although I have tried, I cannot write the code (or at least code that
works), but something like:
if (Form1.Option.value = "Yes") AND (Form1.Name.value == "")
{
alert("Please enter a value for the FIRST NAME field.");
Form1.Name.focus();
return (false);
}
I have tried this but it does not work. I get an "Object Expected"
error.
If anyone can help with a sample code that would accomplish this, I
would appreciate it very much. Thanks.
I use javascript but I think you get the idea anywy
This is called this way
<form .... OnSubmit="return validate( this )">
...
<label
for="namn"
class="label"><b><?php echo $kontakt['Namn']; ?>:</b></label></td>
<input
type="text"
name="namn"
id="namn"
size="35"
value=""
OnBlur="callAjax('namn', this.value, this.id, this.id);">
<span id="req_namn" style="height: 15px;"></span>
<span id="err_namn"></span>
...
</form>
It runs every time someone hit the submit button.
Try it here http://radiomuseum-se.data-doc.se/ [Contakt] button
The "callAjax(...)" part is here http://www.the-art-of-web.com/javascript/ajax/
The "<?php echo ?>" part is text from MySQL database.
I hope you get the idea!
<script language="javascript" type="text/javascript">
function validate_kontakt($form) {
$result = true;
// Check if any contakt info
if ( $form.adress.value <= "" && $form.pnr.value <="" && $form.port.value <= ""
&& $form.tel.value <= "" && $form.epost.value <= "" )
{
$form.adress.style.backgroundColor="lime";
$form.pnr.style.backgroundColor="lime";
$form.port.style.backgroundColor="lime";
$form.tel.style.backgroundColor="lime";
$form.epost.style.backgroundColor="lime";
document.getElementById("err_adress").style.color="red";
document.getElementById("err_adress").style.fontWeight="600";
document.getElementById("err_adress").innerHTML='<?php echo
$kontakt['valerr_adress']; ?><br>';
$form.adress.focus();
return false;
}
else
// Is some contakt info missing
if ( ($form.tel.value <= "" && $form.epost.value <= "") &&
($form.adress.value <= "" || $form.pnr.value <="" || $form.port.value <= "") ) {
if ( $form.adress.value <="" ) {
$form.adress.style.backgroundColor="pink";
document.getElementById("err_adress").style.color="red";
document.getElementById("err_adress").style.fontWeight="600";
document.getElementById("err_adress").innerHTML='<?php echo
$kontakt['valerr_gatadress']; ?> ';
document.getElementById("req_adress").innerHTML='<img src="bild/NotOK.gif"
hspace="3" alt="*" height="15" border="0">';
if ( $result ) $form.adress.focus();
$result = false;
}
if ( $form.pnr.value <="" ) {
$form.pnr.style.backgroundColor="pink";
document.getElementById("err_port").style.color="red";
document.getElementById("err_port").style.fontWeight="600";
document.getElementById("err_port").innerHTML=' <?php echo
$kontakt['valerr_pnr']; ?> ';
document.getElementById("req_pnr").innerHTML='<img src="bild/NotOK.gif"
hspace="3" alt="*" height="15" border="0">';
if ( $result ) $form.pnr.focus();
$result = false;
}
if ( $form.port.value <= "" ) {
$form.port.style.backgroundColor="pink";
document.getElementById("err_port").style.color="red";
document.getElementById("err_port").style.fontWeight="600";
document.getElementById("err_port").innerHTML=' <?php echo
$kontakt['valerr_port']; ?> ';
document.getElementById("req_port").innerHTML='<img src="bild/NotOK.gif"
hspace="3" alt="*" height="15" border="0">';
if ( $result ) $form.port.focus();
$result = false;
}
}
return $result;
}
</script>
Brave skrev: