Skip Navigation

[Resolved] user_login不小於11個字

This thread is resolved. Here is a description of the problem and solution.

Problem:

Validate the user_login field's length.

Solution:

It needs some custom PHP codes, see the solution here:

https://toolset.com/forums/topic/user_login%e4%b8%8d%e5%b0%8f%e6%96%bc11%e5%80%8b%e5%ad%97/#post-1074791

Relevant Documentation:

http://php.net/manual/en/function.mb-strlen.php

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by FuChan 6 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1074713

Hi

請問我有一個註冊表格,我讓它user_login欄位不小於11 個字才可以註冊,我使用cred_form_validate,但放入funtion.php之後,在註冊的時候,輸入超過或等於11個字的時候errors還是會出現訊息,請我的code是否有需要調整的?謝謝

//手機號不小於11個字元
add_filter( 'cred_form_validate', 'tssupp_custom_validation', 10, 2 );
function tssupp_custom_validation( $error_fields, $form_data ) {
   
    //field data are field values and errors
    list( $fields,$errors ) = $error_fields;
   
    //validate if specific form
    if ( $form_data['id'] == 49 ) {
   
        if ( isset( $fields['user_login']['value'] ) && str_word_count( $fields['user_login']['value'] ) < 11 ) {
   
            $errors['user_login'] = '请输入不小于11个字的电话号码';
        }
   }
   
    return array( $fields,$errors );
}

#1074791

你好,

你是用了 function str_word_count(),它是用來計算句子裏面出現的詞語數量,我建議你使用的是另外一個function:mb_strlen()來計算字符數量,例如替換這一行

if ( isset( $fields['user_login']['value'] ) && str_word_count( $fields['user_login']['value'] ) < 11 ) {

為:

if ( isset( $fields['user_login']['value'] ) && mb_strlen( $fields['user_login']['value'] ) < 11 ) {

參考:
hidden link

#1079457

謝謝您,我解決了。