0

我编写了一些验证代码来验证某些表单条目,我尝试使用 onsubmit 命令调用它们,但是当我使用表单时,似乎只有该行中调用的第一个函数有效,并且一旦成功验证了其余的函数被忽略,表单只是提交?这是我的代码:

<script>
function validateFamily()
{
var family=document.getElementById('family');
if (family.value=="")
    {
     alert("Family name must be filled out");
     return false;
    }
else if (document.getElementById('family').value.length > 35)
    {
        alert("Family name cannot be more than 35 characters");
        return false;
    }
}

function validateGiven()
{
var given=document.getElementById('given');
if (given.value=="")
    {
     alert("Given name must be filled out");
     return false;
    }
else if (document.getElementById('given').value.length > 35)
    {
        alert("Given name cannot be more than 35 characters");
        return false;
    }
}

function validDate()
{
var chkdate = document.getElementById("dob").value
if(document.getElementById("dob").value == "")
{
    alert("Please enter Date of Birth")
    return false;
}
else if(!chkdate.match(/^(0[1-9]|[12][0-9]|3[01])[\- \/.](?:(0[1-9]|1[012])[\- \/.](200)[0-4]{1})$/))
    {
      alert('The entered date is an incorrect format');
     return false;
    }
}

function validateMaleFemale() 
{
var radios = document.getElementsByName('malefemale');

for (var i = 0; i < radios.length; i++) 
{
    if (radios[i].checked) 
{
    return true;
}
};
alert ('You must choose male or female');
return false;
}

function validateAddress()
{
var address = document.getElementById('address');
var stringa = document.getElementById('address').value;
if (address.value=="")
    {
     alert("Address must be filled out");
     return false;
    }
else if (document.getElementById('address').value.length > 150)
    {
        alert("Address cannot be more than 150 characters");
        return false;
    }
else if (/[^a-zA-Z0-9\-\/]/.test( stringa ) )
    {
        alert("Address can only contain alphanumeric, hypehns(-) and backslash's(/)")
        return false;
    }
}

function validateParent()
{
var parent = document.getElementById('parenta');
var stringb = document.getElementById('parenta').value;
if (parent.value=="")
    {
     alert("Parent/Carer name must be filled out");
     return false;
    }
else if (document.getElementById('parenta').value.length > 60)
    {
        alert("Parent/Carer name can not be more than 60 characters");
        return false;
    }
else if (/[^a-zA-Z\-\/]/.test( stringb ) )
    {
        alert("Parent/Carer can only contain alphabetic and hypehns(-)")
        return false;
    }
}

function validateWork()
{ 
var num1 = document.getElementById('workno').value;

if (num1 !== "" && !num1.match(/\(\d{2}\)\d{8}/))
{
alert('That is not correct telephone number format');
return false;
}
}

function validateHome()
{ 
var num2 = document.getElementById('homeno').value;

if (num2 !== "" && !num2.match(/\(\d{2}\)\d{8}/))
{
alert('That is not correct telephone number format');
return false;
}
}

function validateMob()
{ 
var num3 = document.getElementById('mob').value;

if(num3 == "")
{
alert('Please enter a mobile number');
return false;
}
else if (num3 !== "" && !num3.match(/\(\d{3}\)\d{8}/))
{
alert('That is not correct mobile number format');
return false;
}
}
</script>

<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return !!(validateFamily() && validDate() && validateAddress() && validateParent() && validateMaleFemale() && validateMob() && validateHome() && validateWork());">

因此,例如,该功能validateFamily()将正常工作并在该字段为空时提醒我,但在该字段中输入某些内容后,其他功能将被忽略并提交表单。谁能告诉我为什么会发生这种情况或如何解决它?

4

1 回答 1

1

编辑:当验证通过时,您的某些验证方法默认不返回值(即):

前任:

    function validateFamily()
    {
       var family=document.getElementById('family');
       if (family.value=="")
       {
           alert("Family name must be filled out");
           return false;
       }
       else if (document.getElementById('family').value.length > 35)
       {
           alert("Family name cannot be more than 35 characters");
           return false;
       }
      return true;// Add this to all validate Methods!
    }

在所有 validateXxx() 方法中添加默认返回值。让我知道这个是否奏效。

于 2013-05-30T10:47:55.953 回答