dslfq06464 2016-10-10 03:17 采纳率: 0%
浏览 58
已采纳

PHP - 我如何计算数字字符和字符串字符来检查用户名长度值?

how to count numerical character combine with string character in php? if i use strlen, thats only count string. I want limit username input value only 20 character, if i input 20 or more string only, this code work, but if i input (e.g : Admin123Admin123Admin123) thats not work, my validation input fail.

i have a code in yii 2 useraccount controller like this :

        // new user

        if ( $username != '' && $password != '' && intval($group) > 0 && !$exist)
        {
            $myFunctions    = new userFunctions;
            $exist = $myFunctions->isUserNameExist( $username );
            $isValid = $myFunctions->isValidPassword( $password );
            $checkUsername = strlen($username);
            // $temp = str_split($username); // Convert a string to an array by each character
            // // if don't want the spaces
            // $temp = array_filter($temp); // remove empty values
            // $checkUsername = count($temp);

            if ( $isValid == 0 && !$exist)
            {
                $result = $myFunctions->saveNewUser( $username, $password, $group, $expired );
                $error =  ( $result ) ? 0 : 1;
            } 
            else if( $exist )
            {
                $error = 3;
            }
            else $error = 2;
        }
    }

    echo \yii\helpers\Json::encode(['result' => $result, 'error' => $error, 'checkUsername' => $checkUsername ]);

this is my code in view :

                function saveNewUsers()
                {
                    $.ajax({
                                type     :'POST',
                                dataType : 'json',
                                data     : { id: $('#hiUserID').val(), username : $('#txtUsername').val(), password: $('#txtPassword1').val(), group: $('#cbUserGroup').val(), expired: $('#cbExpired').val() },
                                url  : '" . \Yii::$app->getUrlManager()->createAbsoluteUrl('useraccount/saveuser') . "',
                                success  : function(response) {
                                    if ( !response.result ) {
                                        if ( response.error == 2 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[20]}.')).show();
                                        }
                                        else if( response.error == 3 )
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[56]}.')).show(); 
                                        }
                                        else if( response.checkUsername > 20)
                                        {
                                            $('#errorMessageUser').html(DecodeEntities('{$myLabels[57]}.')).show();     
                                        }
                                        else $('#errorMessageUser').html(DecodeEntities('{$myLabels[22]}.')).show();
                                    }
                                    else {      
                                        $('#errorMessageUser').html('').hide();
                                        $('#myUserModal').modal('hide');
                                        $.pjax.reload({container:'#myPjax',timeout:false});


                                    }   
                                }                       
                    });
                }

so, how to count numerical and string in php? i am really new in php thanks for helping and i hope suggestion from our programmers here. Sorry for my bad English.

  • 写回答

1条回答 默认 最新

  • doujie1908 2016-10-10 03:33
    关注

    i already found the answer, yes thanks for chris 85, strlen is not problem but the problem is in controller, this is i change my code :

    if ( $username != '' && $password != '' && intval($group) > 0 && !$exist)
            {
                $myFunctions    = new userFunctions;
                $exist = $myFunctions->isUserNameExist( $username );
                $isValid = $myFunctions->isValidPassword( $password );
                $checkUsername = strlen($username);
                // var_dump($checkUsername); die();
    
                if ( $isValid == 0 && !$exist && $checkUsername <= 20)
                {
                    $result = $myFunctions->saveNewUser( $username, $password, $group, $expired );
                    $error =  ( $result ) ? 0 : 1;
                }
                elseif ($checkUsername > 20 ) 
                {
                    $error = 99;
                } 
                else if( $exist )
                {
                    $error = 3;
                }
                else $error = 2;
            }
    

    and in the view like this :

    function saveNewUsers()
                    {
                        $.ajax({
                                    type     :'POST',
                                    dataType : 'json',
                                    data     : { id: $('#hiUserID').val(), username : $('#txtUsername').val(), password: $('#txtPassword1').val(), group: $('#cbUserGroup').val(), expired: $('#cbExpired').val() },
                                    url  : '" . \Yii::$app->getUrlManager()->createAbsoluteUrl('useraccount/saveuser') . "',
                                    success  : function(response) {
                                        if ( !response.result ) {
                                            if ( response.error == 2 )
                                            {
                                                $('#errorMessageUser').html(DecodeEntities('{$myLabels[20]}.')).show();
                                            }
                                            else if( response.error == 3 )
                                            {
                                                $('#errorMessageUser').html(DecodeEntities('{$myLabels[56]}.')).show(); 
                                            }
                                            else if( response.error == 99)
                                            {
                                                $('#errorMessageUser').html(DecodeEntities('{$myLabels[57]}.')).show();     
                                            }
                                            else $('#errorMessageUser').html(DecodeEntities('{$myLabels[22]}.')).show();
                                        }
                                        else {      
                                            $('#errorMessageUser').html('').hide();
                                            $('#myUserModal').modal('hide');
                                            $.pjax.reload({container:'#myPjax',timeout:false});
                                        }   
                                    }                       
                        });
                    }
    

    thanks for helping, finally i get the answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作