Home > Enterprise >  How to validate the fields by combining lambda and regx?
How to validate the fields by combining lambda and regx?

Time:07-02

I want to get a username and password from the user, which should be validated as a function as follows

Terms and conditions for input:

  • Password length greater than or equal to 6.
  • Username length greater than or equal to 4.
  • No one is allowed to join with 'test' and 'codecup' usernames.
  • A user whose password consists only of numbers is not allowed to join.

def check_registration_rules(**kwargs): li = list()

for user,passwd in kwargs.items():
    # print(passwd)
    # print(user)
    if user == 'test' or user == 'codecup':
        continue
    elif len(user) < 4:
        continue
    elif str(passwd).isdigit():
        continue
    elif len(passwd) < 6:
        continue
    else:
        li.append(user)
return li

print(check_registration_rules(username='p', sadegh='He3@lsa'))

Now, instead of these nested conditions, I want to use a combination of lambda and regex library

CodePudding user response:

I'm using lambda and Reg library like a sample:

import re
def check_registration_rules(**kwargs):
  return list((dict(filter(lambda e:(len(e[1])>=6 and len(e[0])>=4 and (e[0] !='test' and e[0]!='codecup') and (not(re.match("^([0-9] ) $",e[1]))) ),kwargs.items()))).keys())
  • Related