validation - 即使 Woocommerce 电话号码和邮政编码字段的输入类型设置为“数字”,某些字母仍然会出现
问题描述
我设法使用此代码调整了我网站的结算结帐字段的一些内容
add_filter( 'woocommerce_default_address_fields', 'custom_woocommerce_default_address_fields' );
function custom_woocommerce_default_address_fields( $address_fields ) {
$address_fields['first_name']['custom_attributes'] = array('maxlength' => 35);
$address_fields['last_name']['custom_attributes'] = array('maxlength' => 35);
$address_fields['address_1']['custom_attributes'] = array('maxlength' => 50);
$address_fields['address_2']['custom_attributes'] = array('maxlength' => 50);
$address_fields['city']['custom_attributes'] = array('maxlength' => 35);
$address_fields['postcode']['custom_attributes'] = array('maxlength' => 4, 'type' => 'number' );
$address_fields['phone']['custom_attributes'] = array('maxlength' => 11, 'type' => 'number' );
unset($address_fields['company']);
unset($address_fields['address_2']);
return $address_fields;
}
请注意我在邮政编码和电话号码字段中所做的两件事。我都为这两个字段设置了特定的最大长度。我还将他们的输入类型设置为“数字”,因为我注意到在这些应该只是数字的字段中也可以输入字母。但是当我运行这段代码时没有任何反应。
我还尝试分离我为这些字段所做的自定义,如下所示:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields )
{
$fields['billing']['billing_phone']['type'] = 'number';
$fields['billing']['billing_postcode']['type'] = 'number';
$fields['billing']['billing_phone']['maxlength'] = 11;
$fields['billing']['billing_postcode']['maxlength'] = 4;
return $fields;
}
但问题只是['type'] = 'number'是这两个结帐字段中的可见变化。我认为这没问题,但是当我测试输入所有字母时,字母e和r仍然出现在邮政编码和电话字段中。这是一个错误还是有可能克服这个问题?我只想在这两个计费字段中同时实现 maxlength 和 input type = number。
解决方案
推荐阅读
- python - Visual Studio Code 告诉我它无法导入 python 模块,但它运行代码
- c++ - 我的 word search II 解决方案很慢(Leetcode 212)
- javascript - 在 OnboardingScreens 上找不到 ID 为“kotlin-android”的插件
- laravel - 如何使用 VUE JS 访问 laravel API?
- html - 试图制作一个命令页面来列出我的不和谐机器人上的所有命令,但不知道如何
- javascript - “(组件)未定义”,找不到错误?
- python - 扩展 Marshmallow 架构但覆盖字段必需属性
- python - 在 C++ 脚本中嵌入 Python,PyRun_SimpleFile 无法按预期工作
- kotlin - 将两个状态流组合成新的状态流
- firebase - Flutter/Firestore - 实时收听集合和子集合