【问题标题】:Gravity forms validation on number field数字字段上的重力形式验证
【发布时间】:2014-10-28 06:04:01
【问题描述】:

重力表单电话号码字段应该验证电话号码,但如果字段选项设置为“国际”,则如果字段中的数据是标准字符,则表单提交。

下面的代码与我的表单和特定字段挂钩,但我遇到了如何检查字段字符串是否为数字的问题。

// add custom validation to the gravity forms plugin to validate "phone number" field
add_filter("gform_field_validation_2_4", "custom_validation", 10, 4);

function custom_validation($result, $value, $form, $field){

if($result["is_valid"] && intval($value)){

$result["is_valid"] = false;

$result["message"] = "Please enter a valid telephone number";

}
return $result;
}

非常感谢您的建议和反馈。

谢谢, JB.

【问题讨论】:

    标签: php wordpress forms gravity-forms-plugin


    【解决方案1】:

    为什么不尝试使用正则表达式?它适用于这种情况。例如(未经测试):

    // add custom validation to the gravity forms plugin to validate "phone number" field
    add_filter("gform_field_validation_2_4", "custom_validation", 10, 4);
    
    function custom_validation($result, $value, $form, $field){
    
        if(!preg_match('~^\d+$~', $value)){
            $result["is_valid"] = false;
            $result["message"] = "Please enter a valid telephone number";
        }
    
        return $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-13
      • 2017-06-04
      • 1970-01-01
      • 2013-06-16
      • 1970-01-01
      • 2020-05-10
      • 2016-08-01
      相关资源
      最近更新 更多