简体   繁体   English

用于电子邮件比较/验证的PHP代码

[英]PHP Code for Email Comparison / Verification

I am trying to create a function to compare two email fields. 我正在尝试创建一个功能来比较两个电子邮件字段。

As in : 如:

Email : <input type="text" name="email" id="email" value="<?=$email?>"    
required> 

Confirm email : <input type="text" name="email2" id="email2" 
onblur="confirmEmail()" value="<?=$email2?>" required>

Here is the JavaScript code I inserted into my HTML : 这是我插入HTML的JavaScript代码:

 <script type="text/javascript">
 function confirmEmail() {
 var email = document.getElementById("email").value
 var email2 = document.getElementById("email2").value
 if (email != email2) {
 alert('Email Not Matching!');  }
    }
 </script>

The code works. 代码有效。

Once the user enters the second email address, localhost displays an alert, saying : " Email not matching " 用户输入第二个电子邮件地址后,本地主机将显示警报,提示:“ 电子邮件不匹配

For extra-measure, I inserted the following into the form's properties : onsubmit="return confirmEmail() 作为额外的措施,我在表单的属性中插入了以下内容: onsubmit =“ return ConfirmEmail()

So, if the user ignores the first warning, he gets a second warning when he tries to press the SUBMIT button. 因此,如果用户忽略第一个警告,则当他尝试按SUBMIT按钮时会收到第二个警告。

Unfortunately, this is where I am stuck. 不幸的是,这就是我所困的地方。 Because : after the second warning, if the user still does not modify the "confirm email" , the SUBMIT button still works The form gets sent. 因为:在第二次警告后,如果用户仍然不修改“确认电子邮件”,则“提交”按钮仍然有效。表单被发送。

How can I modify the code, so that : the error message continues to display until the user changes the email2 correctly?? 如何修改代码,以便:错误消息继续显示,直到用户正确更改email2为止?

(I tried using the WHILE function, and the DO....WHILE function. They worked............except that, the error-message kept displaying over and over.........and did not allow me to make the required correction to the email field (haha). I had to close the window completely) (我尝试使用WHILE函数和DO .... WHILE函数。它们起作用了......除了错误消息一遍又一遍地显示外…… ...并且不允许我对电子邮件字段(haha)进行必要的更正。我不得不完全关闭窗口)

I would submit the form with JavaScript. 我将使用JavaScript提交表单。

<form>
  Email : <input type="text" name="email" id="email" value="<?=$email?>"    
required> 

  Confirm email : <input type="text" name="email2" id="email2" 
onblur="confirmEmail()" value="<?=$email2?>" required>
</form>

<button onclick="formSubmit()">Learn More</button>

Your formSubmit() function would simply pull in the values and submit them after the proper check. 您的formSubmit()函数将简单地提取值并在正确检查后提交它们。 This way, regardless of what the user enters, it has to go through your verification before it is submitted. 这样,无论用户输入什么内容,都必须先进行验证才能提交。

function formSubmit() {
  var email = document.getElementById("email").value;
  var email2 = document.getElementById("email2").value;
  if (email != email2) {
  alert('Email Not Matching!');  
} else {
  if (window.XMLHttpRequest) {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.open("GET","/Your/Path/To/Your/Form-Processing/?email="+email,true);
  xmlhttp.send();
}

The function above will go in your script tags. 上面的函数将在您的脚本标签中。 It checks email and email2 against each and then submits email with GET to the processing page the same way your form would. 它针对每个emailemail2进行检查,然后以与表单相同的方式将带有GET的email提交到处理页面。 You can also pass other variables the same way by getting them with document.getElementById('#id').value and then send them through the GET method. 您也可以通过使用document.getElementById('#id').value获取其他变量,然后通过GET方法发送它们,以相同的方式传递其他变量。

First, give your submit button an ID like this: 首先,为您的提交按钮提供一个ID,如下所示:

<input type="submit" value="Submit" id="mySubmit" disabled="disabled">

And in your if block add: 并在您的if块中添加:

if (email !== email2) {
  alert('Not matching')
  document.getElementById("mySubmit").disabled = true;
}else{
  document.getElementById("mySubmit").disabled = false;
}

What you could do is: 你能做的是:

 <script type="text/javascript">
 var count=0;

 function confirmEmail() {
 var email = document.getElementById("email").value
 var email2 = document.getElementById("email2").value

 function chkEmail(){
     if (email != email2 && count==0)
       { alert('Email Not Matching!'); count++ }
     else if(email!=email2 && count ==1)
       //display warning
   }
chkEmail();
    }
 </script>

try this bro, 试试这个兄弟

<form name="form" method="post" action="">
Email : <input type="text" name="email" id="email" value="<?=$email?>" required> 

Confirm email : <input type="text" name="email2" id="email2" value="<?=$email2?>" required>
<input type="submit" value="submit" onsubmit="return confirmEmail();"/>

</form>

and in java script 和在Java脚本中

 <script type="text/javascript">


 function confirmEmail() {
 var email = document.getElementById("email").value;
 var email2 = document.getElementById("email2").value;


     if (email != email2)
       { alert('Email Not Matching!');  return false; }
     else {
       return true;
      }

   }

 </script>

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

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