Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python ตรวจสอบความถูกต้องของ Password?


ที่นี่ให้รหัสผ่าน หน้าที่ของเราคือตรวจสอบว่ารหัสผ่านนี้ถูกต้องหรือไม่ ที่นี่เราใช้ re module ที่ให้นิพจน์ทั่วไป และ re.search() ใช้สำหรับตรวจสอบความถูกต้องของตัวอักษร ตัวเลข หรืออักขระพิเศษ

อัลกอริทึม

Step 1: first we take an alphanumeric string as a password.
Step 2: first check that this string should minimum 8 characters.
Step 3: the alphabets must be between a-z.
Step 4: At least one alphabet should be in Uppercase A-Z.
Step 5: At least 1 number or digit between 0-9.
Step 6: At least 1 character from [_ or @ or $].

โค้ดตัวอย่าง

# Python program to check valid password 
import re 
passw = input("Enter Password ::>")
fl = 0
while True:   
   if (len(passw)<8): 
      fl= -1
      break
   elif not re.search("[a-z]", passw): 
      fl = -1
      break
   elif not re.search("[A-Z]", passw): 
        fl = -1
        break
    elif not re.search("[0-9]", passw): 
      fl = -1
      break
   elif not re.search("[_@$]", passw): 
      fl = -1
      break
   elif re.search("\s", passw): 
      fl = -1
      break
   else: 
      fl = 0
      print(" This Is Valid Password") 
      break
if fl ==-1: 
   print("Not a Valid Password") 

ผลลัพธ์

Enter Password ::> vbnA@hj9
This Is Valid Password