简体   繁体   English

PHP验证和Javascript验证不能一起使用

[英]PHP Validation and Javascript Validation won't work together

When I combine my php form validation code with my javascript validation code, the javascript code fails to work when I hit the submit button. 当我将php表单验证代码与javascript验证代码结合使用时,当我按下“提交”按钮时,javascript代码无法正常工作。 It will only validate the first form field and not the 3 others and then php will validate all fields. 它只会验证第一个表单字段,而不验证其他三个字段,然后php将验证所有字段。 I don't want the php form validation to do anything until javascript has completed the form validation. 在JavaScript完成表单验证之前,我不希望php表单验证执行任何操作。

When I use only php or only javascript to validate, then the code works correctly. 当我仅使用php或仅使用javascript进行验证时,代码即可正常工作。 What am I missing here? 我在这里想念什么? Is it something to do with the beginning of the form? 与表格开头有关吗?

"form name="contactform" id="contactform" method="post" 
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" 
onsubmit="return validateentry();">"

Am I supposed to do the php form validation while "action" goes to a different web page? 我应该在“操作”转到另一个网页时进行php表单验证吗?

The javascript code: javascript代码:

function validateemail()
{
var emailentry=document.forms.contactform.email.value;
at=emailentry.indexOf("@");
period=emailentry.lastIndexOf(".");
if(at < 1 || ( period - at < 2 )) 
{
   alert("Please enter correct email in the format of 'yourmail@yourwebsite.com'")
   document.forms.contactform.email.focus();
   return false;
}
return(true);
}


function validatephonenumber()
{
var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/;  
    var numbers = document.forms.contactform.phone.value; 
    var verified = re.exec(numbers);  
    if (!verified)  
    {
alert("Please enter a phone number in the format of 999-999-9999");
return false;
}  
    return(true); 
}

function validateentry()
{
if(document.forms.contactform.name.value=="")
{
 alert("Please provide your name.");
 document.forms.contactform.name.focus();
 return false;
}

if(document.forms.contactform.company.value=="")
{
 alert("Please provide your company name. If you don't have one, simply state 
 that you don't.");
 document.forms.contactform.company.focus();
 return false;
}

 if(document.forms.contactform.email.value == "")
 {
 alert("Please provide an Email address.");
 document.forms.contactform.email.focus();
 return false;
 }else{
 var validformat=validateemail();
 if(validformat==false)
 {
 return false;
 }}

if(document.forms.contactform.phone.value=="")
{
 alert("Please provide a phone number in the format 999-999-9999.");
 document.forms.contactform.phone.focus();
 return false;
}
else if(document.forms.contactform.phone.value.length < 12)
{
    alert("Please provide the phone number in the format of 999-999-9999.");
    document.forms.contactform.phone.focus();
        return false;
}
    else
    {
    var validnumber=validatephonenumber();
    if(validnumber==false)
    {
    return false;
    }}

if(document.forms.contactform.msg.value=="")
{
 alert("Please provide a message.");
 document.forms.contactform.msg.focus();
 return false; 
}
 return(true);
}

It's unclear without more code but based on your comment I am going to guess that you have incorrectly written your php and it's breaking your javascript/html code. 尚不清楚,如果没有更多代码,但根据您的评论,我想您可能是错误地编写了php,这破坏了javascript / html代码。 Perhaps one of your quotes? 也许你的报价之一? Look at the source code of the page and run it through one of the online validation services such as http://validator.w3.org and http://www.jslint.com 查看页面的源代码,并通过一种在线验证服务(例如http://validator.w3.orghttp://www.jslint.com)运行它

Try this: 尝试这个:

PHP HTML: PHP HTML:

<?php
echo "<form name='contactform' id='contactform' method='post' 
action='' onsubmit='return validateentry(this);'>"
...

Validation JavaScript: 验证JavaScript:

function validateemail(e)
{
    var emailentry = e.value
        , at = emailentry.indexOf("@")
        , period = emailentry.lastIndexOf(".");

    if(at < 1 || ( period - at < 2 )) 
    {
       alert("Please enter correct email in the format of 'yourmail@yourwebsite.com'")
       e.focus();
       return false;
    }
    return true;
}


function validatephonenumber(e)
{
    var re = /(?:\d{3}|\(\d{3}\))([-\/\.])\d{3}\1\d{4}/
        , numbers = e.value;

    if (!re.exec(numbers))
    {
        alert("Please enter a phone number in the format of 999-999-9999");
        e.focus();
        return false;
    }
    return true;
}

function validateentry(f)
{
    if(f.name.value == "")
    {
        alert("Please provide your name.");
        f.name.focus();
        return false;
    }

    if(f.company.value == "")
    {
        alert("Please provide your company name. If you don't have one, simply state 
        that you don't.");
        f.company.focus();
        return false;
    }

    if(f.email.value == "")
    {
        alert("Please provide an Email address.");
        f.email.focus();
        return false;
    }
    else
    {
        var validformat = validateemail(f.email);
        if(validformat == false)
        {
            return false;
        }
    }

    if(f.phone.value == "" || f.phone.value.length < 12 || (validnumber = validatephonenumber(f.phone)) == false)
    {
        alert("Please provide the phone number in the format of 999-999-9999.");
        f.phone.focus();
        return false;
    }

    if(f.msg.value == "")
    {
        alert("Please provide a message.");
        f.msg.focus();
        return false; 
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM