This is surely the 100th problem with validate functions before a form is submitted, but the others doesn't help me:
I have a form, witch, at the opening of the page, does have dynamically assigned the 'onblur' property using:
This works perfectly. when the user enters a character into a field where normally there only can ben numbers, the border of the field will become red, and the user must correct his fault before he can go to an other field.
also, when a field has a yellow backgrond color, the field is required, so I have a function called beforeSubmit that checks for yellow fields.
It also performs the check for a red bordercolor using the function above, because although a user cannot go out of the field with a wrong value, it do can click on the submit button. code:
The problem is, that when a user enters a wrong value into a field an presses enter instead of click the submit button, the validation function is not called.
This is what the form looks like:
I tried to rename the onclick function on the button to onsubmit, I even tried the onsubmit in the form tag, but nothing helps.
What am I doing wrong?
Tom
I have a form, witch, at the opening of the page, does have dynamically assigned the 'onblur' property using:
Code:
function addEvents(formName, onWhat)
{
theForm = document.getElementById(formName, onWhat)
if (theForm == null)
return;
for (x=0; x<theForm.elements.length; x++)
theForm.elements[x].attachEvent(onWhat, eval(onWhat+'Function'))
}
function onblurFunction()
{
myFormField = event.srcElement;
if (myFormField.name == 'ABO_PK' || myFormField.name == 'SRE_PK' || myFormField.name == 'SRE_HNR1')
if (!isInteger(myFormField.value))
showFalseField(myFormField, '<?php echo __BORDERINCORRECTFIELD ?>');
else
showGoodField(myFormField, '<?php echo __BORDERCORRECTFIELD ?>');
if (myFormField.name == 'SRE_PC')
myFormField.value = checkPostcode(myFormField.value);
}
function showFalseField(veld, border)
{
veld.focus();
veld.style.border = border;
}
function showGoodField(veld, border)
{
veld.style.border = border;
}
This works perfectly. when the user enters a character into a field where normally there only can ben numbers, the border of the field will become red, and the user must correct his fault before he can go to an other field.
also, when a field has a yellow backgrond color, the field is required, so I have a function called beforeSubmit that checks for yellow fields.
It also performs the check for a red bordercolor using the function above, because although a user cannot go out of the field with a wrong value, it do can click on the submit button. code:
Code:
function beforeSubmit(formName, requiredFieldBgColor)
{
theForm = document.getElementById(formName);
for (x=0; x<theForm.elements.length; x++)
{
if (theForm.elements[x].style.background == requiredFieldBgColor && theForm.elements[x].value == '')
{
alert("You have some required (yellow) fields without a value.");
return false;
}
if (theForm.elements[x].style.border == 'red 2px solid')
{
alert ("You have a field witch contains a wrong value (with a red border).");
return false;
}
}
return true;
}
The problem is, that when a user enters a wrong value into a field an presses enter instead of click the submit button, the validation function is not called.
This is what the form looks like:
Code:
<form action="index2.php" method="POST" name="zoek_form">
Abonnementnummer:<br>
<input type="text" name="ABO_PK" class="text" style="width: 200px;"><br><br>
Relatienummer:<br>
<input type="text" name="SRE_PK" class="text" style="width: 200px;"><br><br>
Naam:<br>
<input type="text" name="SRE_ZK" class="text" style="width: 200px;"><br><br>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Postcode:<br>
<input type="text" name="SRE_PC" value maxlength="7" class="text" style="width: 70px;">
</td>
<td width="10"> </td>
<td>Huisnummer:<br>
<input type="text" name="SRE_HNR1" class="text" style="width: 60px;">
</td>
</tr>
</table>
<br>
Telefoonnummer:<br>
<input type="text" name="SRE_TEL" class="text" style="width: 200px;">
<br>
Zoeken op:<br>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="120">
<input type="radio" name="abo_deb" value="abo" checked>Abonnee
</td>
<td>
<input type="radio" name="abo_deb" value="deb">Debiteur
</td>
</tr>
</table>
<br>
<input type="Submit" value="OK" onclick="return beforeSubmit('zoek_form', '<?php echo __REQUIREDFIELDBGCOLOR ?>')">
^php variabele holding a bordercolor
I tried to rename the onclick function on the button to onsubmit, I even tried the onsubmit in the form tag, but nothing helps.
What am I doing wrong?
Tom