簡體   English   中英

如果滿足特定條件,如何檢查變量是否為字母數字

[英]How to check if a variable is alphanumeric if a certain condition has been met

我正在嘗試檢查用戶用戶名的長度是否為 7。如果滿足條件,那么我想檢查它是否為字母數字。 如果不是,我想返回下面的錯誤。

您輸入了無效的用戶名

這是我的代碼

def clean(self):
        super(Register, self).clean()
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        username = self.cleaned_data.get('username')
        email = self.cleaned_data.get('email')

        
        if password.islower():
            self.errors[''] = self.error_class(["Password must contain an uppercase"])
        
        elif password.isnumeric():
            self.errors[''] = self.error_class(["Password must contain an alphabet"])

        elif password.isalpha():
            self.errors[''] = self.error_class(["Password must contain a number"])

        elif len(password) < 8:
            self.errors[''] = self.error_class(["Password is too short"])

        elif password != password2:
            self.errors[''] = self.error_class(["The two password fields must match"])

        elif len(username) !=7  and len(username) != 9:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        else:
            pass
        
        try:
            if len(username) == 9:
                username = int(username)
        except ValueError:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        if len(username) == 7:
            if username.isalnum() is False:
                self.errors[''] = self.error_class(["Youuuu have entered an invalid username"])
            else:
                pass
        else:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        for instance in User.objects.all():
            if instance.username == username:
                self.errors[''] = self.error_class(["User already exist"])
            elif instance.email == email:
                self.errors[''] = self.error_class(["E-mail already in use"])
            else:
                pass

        return self.cleaned_data

沒有空間將其粘貼到評論中,因此我將嘗試先回答問題。

你定義 class 的語法是錯誤的,它應該看起來像這樣......

我猜 class 將被稱為 Clean,而 Clean class 是從寄存器 class 派生的嗎?

class Clean(Register):
    def __init__(self):
        super().__init__().clean()
        password = self.cleaned_data.get('password')
        password2 = self.cleaned_data.get('password2')
        username = self.cleaned_data.get('username')
        email = self.cleaned_data.get('email')

    
        if password.islower():
            self.errors[''] = self.error_class(["Password must contain an uppercase"])
        
        elif password.isnumeric():
            self.errors[''] = self.error_class(["Password must contain an alphabet"])

        elif password.isalpha():
            self.errors[''] = self.error_class(["Password must contain a number"])

        elif len(password) < 8:
            self.errors[''] = self.error_class(["Password is too short"])

        elif password != password2:
            self.errors[''] = self.error_class(["The two password fields must match"])

        elif len(username) !=7  and len(username) != 9:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        else:
            pass
        
        try:
            if len(username) == 9:
                username = int(username)
        except ValueError:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        if len(username) == 7:
            if username.isalnum() is False:
                self.errors[''] = self.error_class(["Youuuu have entered an invalid username"])
            else:
                pass
        else:
            self.errors[''] = self.error_class(["You have entered an invalid username"])

        for instance in User.objects.all():
            if instance.username == username:
                self.errors[''] = self.error_class(["User already exist"])
            elif instance.email == email:
                self.errors[''] = self.error_class(["E-mail already in use"])
            else:
                pass

        return self.cleaned_data

所以弄清楚你到底想首先實例化什么。 我們可以盡力提供幫助。

如果你只是想看看你問的問題的邏輯是什么樣的。 它應該看起來像這樣。 首先你需要在 class 中創建一個名為“角色”的新變量,這樣你就可以存儲這個人是學生還是老師。 然后你可以在 class 內執行此操作。

if self.role == 'teacher':
    if len(self.id) == 7 and self.id.isalnum():
        print('success')
    else:
        print('whatever error code you want here')

elif self.role == 'student':
    if len(self.id) == 9 and self.id.isnumeric():
        print('success')
    else:
        print('whatever error code you want here')

else:

    print('error no role given')

這只是一個起點,因為您在那里創建的 class 很亂。 但是你應該能夠使用它來創建你想要的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM