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.