douyi9597 2017-10-17 08:47
浏览 90
已采纳

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

This is my website URL http://saloon.ekvitech.com/schedule-appointment/ on this page I am using a 'easy-appointment-plugin' of WordPress to book an appointment. Here when you fill the appointment form step by step then you are towards to personal information form. When you fill this then the form is submitted for review by admin. But the problem is in that form there is a phone field and that field was accepting alphabet as well as digits. This should be not done with the phone as we know that phone number will accept only digits.

So I need to validate this field with digits only. If someone enters alphabets then error message arrises digits only.

I applying my code in the "header.php" file, I am stuck with my script code because the fields are autogenerated by WordPress plugin and all fields have same class how we can achieve this and validate should be done on single filed i.e. on phone field only.

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

Note: My script will hide all fields don't know why. Please help me out form this sticky situation.

Many Thanks!

</div>
  • 写回答

1条回答 默认 最新

  • dongtu7205 2017-10-17 11:05
    关注

    try this code:

    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;
        }
       });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏