如何在电话字段中禁止重复号码,如1111111或222222



我使用联系人表单7,希望禁止用户在电话字段中输入重复的数字,如1111111或2222222。

我使用下面的代码只输入10位数字。有没有人可以帮助我,我应该改变或添加在这个工作。

// define the wpcf7_is_tel callback<br> 
function custom_filter_wpcf7_is_tel( $result, $tel ) {<br> 
$result = preg_match( '/^(?+?([0-9]{0})?)?[-. ]?(d{10})$/', $tel );<br>
return $result; <br>
}<br>

add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );

首先,[0-9]{0}看起来像一个错字,因为该模式不匹配任何内容,而是一个空字符串。你可能想匹配一个可选的区号,三位数。所以,如果你的意思是,使用d{3}

接下来,要禁止在与d{10}匹配的数字中包含相同的数字,只需将其重写为(?!(d)1{9})d{10}

考虑到上面所说的,解决方案是

function custom_filter_wpcf7_is_tel( $result, $tel ) {<br> 
return preg_match('/^(?+?(?:d{3})?)?[-. ]?(?!(d)1{9})d{10}$/', $tel );<br>
}

参见regex演示。

最新更新