1

I have this code in python. Actually i wanted to validate the password field at server side using python. i want password to be alphanumeric in nature and thats what i have done so far

def valid():
    num = "[a-zA-Z0-9]"
    pwd = "asdf67AA"
    match  = re.match(pwd, num)
    if match:
        return True
    else:
        return False

its always returning false . It does not hit the IF condition. whats that i am missing

5 Answers 5

2

You reversed the arguments of match

re.match(pattern, string, flags=0)

It should be

match  = re.match(num, pwd)
Sign up to request clarification or add additional context in comments.

Comments

1

You can do this to find out if the string is alphanumeric

pwd = "asdf67AA"
if pwd.isalnum():
    # password is alpha numeric
else:
    # password is not alpha numeric

2 Comments

its very much right but what it we want to include special characters like @#$ etc....in that case u have to take help of regular expressions
Yeah, for checking complex cases you have to use re.
0

You have miss placed the argument in method reverse it .

it will work.

But this also matched if you set symbol in your string. so, i suggest you apply this things :

pwd ="test5456"

pwd.isalnum():

This method will only work for if your string contain string or digit.

if you want strongly want your password contain both numeric and string than this code will help you.

 test = "anil13@@"
 new = tuple(test)
 data = list(new)
 is_symb =False
 is_digit = False
 is_alpha = False

 for d in data :
    if d.isdigit() :
       is_digit = True
    if d.isalpha():
       is_alpha = True
    if not d.isdigit() and not d.isalpha():
       is_symb =True
       break
if is_symb == False and is_digit == True and is_alpha == True:
   print "pwd matchd::::"
else :
   print "pwd dosen't match..."

Note :This code strongly work for numeric & symbol

Regards, Anil

2 Comments

its great...how can we modify it for symbols like $ or # or % etc
replace last If condition with below. if is_symb == True and is_digit == True and is_alpha == True: print "pwd matchd::::"
0

This is very simple answer and it is better to use regex for validation instead of wired if else condition.

import re
password = raw_input("Enter Your password: ")
if re.match(r'[A-Za-z0-9@#$%^&+=]{6,}', password):
    print "Password is match...."
else:
    print "Password is not match...."
#6 means length of password must be atleast 6

Comments

-1

you can do it by using isalnum() method of python just by doing

pwd.isalnum()

it will tell wthether a number is alphanumeric or not and then will hit the if condiion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.