我想要一个函数,如果输入的数字不是+91-1234567890格式,它会发出警告。
以下代码在下面列出的每个电话号码上都有错误
- +911234567890
- +901234567890
- 0987654321
- 001987654321
if (preg_match('^+?[9]{1}[1]{1}-?[789]d{9}$')!== $_POST['billing_phone']) {
wc_add_notice( __( 'The Phone field should be <strong>10 digits</strong>.' ), 'error' );
}
要只匹配+91-1234567890
类型的格式,只需匹配后面紧跟10位数字的+91-
即可。
^+91-d{10}$
代码如下:
if (preg_match('/^+91-d{10}$/',$_POST['billing_phone']) !== 1){
wc_add_notice( __( 'The Phone field should be <strong>10 digits</strong>.' ), 'error' );
}