检查输入值是否等于整数然后不提交表单 [英] check if input value not equal to integer then don't submit form

查看:95
本文介绍了检查输入值是否等于整数然后不提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的php电子邮件表单,但我收到垃圾邮件。我添加了一个检查输入,用户必须填写输入。如果输入值等于12,那么提交表单,否则不要提交表单的弹出错误。(请做数学计算。)

 < form action =method =post> 
< input type =textname =nameplaceholder =namerequired>
< input type =textname =emailplaceholder =emailrequired>
< input type =textname =phoneplaceholder =phonerequired>

< div class =check>
< label> 6 x 2 =< / label>
< input type =textname =not_robotrequired =required>
< / div>

< input type =submitvalue =submit>
< / form>

我使用下面的php方法:

  if(isset($ _ POST ['not_robot'])!== 12&& isset($ _ POST ['not_robot'])!==''){
echo'< script type =text / javascript> alert(请做数学运算,如果你是人类的话。);< / script>';
} else {
//电子邮件脚本
}

当我提交表单时,错误弹出窗口会出现请做数学运算,如果你是人类,但在关闭弹出窗口后,它也会发送电子邮件。
任何帮助appreaciated谢谢


PS:如果检查方法可能使用javascript或jquery,这将是一个很大的帮助。

解决方案

尝试检查提交表单时的情况。使用下面的代码或链接 -



JSFiddle

a>

HTML代码 -

 < form action = method =post> 
< input type =textname =nameplaceholder =namerequired>
< input type =textname =emailplaceholder =emailrequired>
< input type =textname =phoneplaceholder =phonerequired>

< div class =check>
< label> 6 x 2 =< / label>
< input type =textname =not_robotrequired =required>
< / div>

< input type =submitvalue =submit>
< / form>

JAVASCRIPT代码 -

  $('form')。submit(function(){
if(parseInt($('input [name =not_robot]')。val())== 12){
返回true;
}
else {
alert('您没有输入正确的值');
返回false;
}
});


i have a simple php email form, but i am getting spam emails. 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.)

<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:

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

P.S: if check method is possible using javascript or jquery it would be a great help.

解决方案

Try to check when submitting the form. Go with below code or link-

JSFiddle

HTML Code-

<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-

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

这篇关于检查输入值是否等于整数然后不提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆