我们如何将验证应用于WordPress插件的任何字段

时间:2017-10-17 08:47:59

标签: javascript php jquery wordpress forms

这是我在此页面上的网站网址http://saloon.ekvitech.com/schedule-appointment/我正在使用WordPress的“易于预约 - 插件”来预约。在这里,当您逐步填写预约表格时,您将转向个人信息表格。当您填写此表单时,表单将提交给管理员进行审核。 但问题是这种形式有一个电话字段,该字段接受字母和数字。这应该不是通过电话完成的,因为我们知道电话号码只接受数字。

所以我需要仅使用数字验证此字段。如果有人输入字母,那么错误消息只会产生数字。

我在“header.php”文件中应用我的代码,因为字段是由WordPress插件自动生成的,所有字段都有相同的类,我们如何实现这一点和验证应该在单一领域进行,即仅在电话领域。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script>
jQuery(document).ready(function () {
  //called when key is pressed in textbox
  jQuery(".custom-field").keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        jQuery(".col-sm-8").html("Digits Only").show().fadeOut("slow");
               return false;
    }
   });
});
</script>

注意:我的脚本会隐藏所有不知道原因的字段。请帮我解决这个棘手的情况。

非常感谢!

1 个答案:

答案 0 :(得分:0)

试试这段代码:

jQuery(document).ready(function () {
  //called when key is pressed in textbox
  jQuery(document).on("keypress","input[name='phone']",function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
        //display error message
        alert('number only');
               return false;
    }
   });
});