简体   繁体   English

检查输入值是否不等于整数,然后不提交表单

[英]check if input value not equal to integer then don't submit form

i have a simple php email form, but i am getting spam emails. 我有一个简单的php电子邮件表格,但是我收到了垃圾邮件。 I have added a check input, User have to fill the input. 我添加了检查输入,用户必须填写输入。 If input value equal to 12 then submit the form, otherwise don't submit the form with a popup error.(Please do the math.) 如果输入值等于12,则提交表单,否则不要提交带有弹出错误的表单。(请进行数学计算。)

<form action="" method="post">
<input type="text" name="name" placeholder="name" required>
<input type="text" name="email" placeholder="email" required>
<input type="text" name="phone" placeholder="phone" required>

<div class="check">
   <label>6 x 2 =</label>
   <input type="text" name="not_robot" required="required">
</div>

<input type="submit" value="submit">
</form>

i am using php method below: 我正在使用以下php方法:

if(isset($_POST['not_robot']) !== 12 && isset($_POST['not_robot']) !== ''){
    echo '<script type="text/javascript">alert("Please do the math, if you are human.");</script>';
}else{
    //email script here
}

when i submit form, error popup appear saying "Please do the math, if you are human", but after i close the popup, it also send email. 当我提交表单时,错误弹出窗口出现,提示“请算一下,如果您是人类”,但是在我关闭弹出窗口后,它也会发送电子邮件。 Any help appreaciated Thanks 任何帮助感激

PS: if check method is possible using javascript or jquery it would be a great help. PS:如果可以使用javascript或jquery进行检查,那将是一个很大的帮助。

You need to test on the client: 您需要在客户端上进行测试:

Plain JS 普通JS

window.onload=function() {
  document.querySelector("form").onsubmit=function(e) {
    var val = this.elements["not_robot"].value;
    return !isNaN(val) && parseInt(Number(val)) == val &&  !isNaN(parseInt(val, 10);
  }
}

jQuery: jQuery的:

$(function() {
  $("form").on("submit",function(e) {
    var val = $("[name='not_robot'"].val();
    if (isNaN(val) || !parseInt(Number(val)) == val ||  isNaN(parseInt(val, 10)) e.preventDefault();
  }
}

Don't try to prevent sending spam mail this way. 不要试图阻止这种方式发送垃圾邮件。 My suggestion is to apply csrf protection and also google captcha. 我的建议是应用csrf保护以及Google验证码。 You can use this library for csrf protection. 您可以将此库用于csrf保护。 And for google captcha use this 对于谷歌验证码使用

Try to check when submitting the form. 提交表单时请尝试检查。 Go with below code or link- 使用以下代码或链接-

JSFiddle 的jsfiddle

HTML Code- HTML代码

<form action="" method="post">
  <input type="text" name="name" placeholder="name" required>
  <input type="text" name="email" placeholder="email" required>
  <input type="text" name="phone" placeholder="phone" required>

  <div class="check">
    <label>6 x 2 =</label>
    <input type="text" name="not_robot" required="required">
  </div>

  <input type="submit" value="submit">
</form>

JAVASCRIPT Code- JAVASCRIPT代码-

$('form').submit(function() {
      if (parseInt($('input[name="not_robot"]').val()) == 12) {
        return true;
      }
      else{
        alert('You not enterd the correct value');
        return false;
      }
    });

If you want to validate through PHP..Below is the way 如果要通过PHP验证,下面是方法

if(is_numeric($_POST['not_robot']) && !empty($_POST['not_robot'])){
    echo '<script type="text/javascript">alert("Please do the math, if you are human.");</script>';
}else{
    //email script here
}

Ways : 方法 :

  • Use PHP exit() function to exits the current script execution.we can use this function with (or without) an message. 使用PHP exit()函数退出当前脚本执行。我们可以在有(或没有)消息的情况下使用此函数。

    Syntax : exit(message) OR exit() 语法: exit(message) OR exit()

  • We can also use return; 我们也可以使用return;

    Here, the control will return to the script that invoked the running of that file. 在这里,控件将返回到调用该文件运行的脚本。

Try this : 尝试这个 :

if(isset(($_POST['not_robot']) != '12') && (isset($_POST['not_robot']) !== '')){
    /* alert here */
    exit();
}else{
    //email script here
}

HTML: HTML:

<form action="" method="post" name="form1">
<input type="text" name="name" placeholder="name" required>
<input type="text" name="email" placeholder="email" required>
<input type="text" name="phone" placeholder="phone" required>

<div class="check">
   <label>6 x 2 =</label>
   <input type="text" name="not_robot" id="not_robot" required="required" />
</div>

<input type="button" value="submit" onClick="checkForm()">
</form>

JavaScipt: javascipt的:

function checkForm()
{

  if(document.getElementById('not_robot').value == "12" && document.getElementById('not_robot').value != "")
  {
    alert('ok');
    document.forms['form1'].submit();
  }else{
        alert('not ok');
      }

}

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

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