5 Replies - 1686 Views - Last Post: 19 May 2012 - 07:50 PM Rate Topic: -----

#1 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

validation of entered of user name

Posted 17 May 2012 - 11:51 PM

I've to validate the entered user name but I do not know how to do it.regex might be like this Regex(@"^[A-z][A-z|\.|\s]+$"); but I do not know how to implement regular expression in python via a class.
class name:
def __int__(name):
		self.name = name
	def validateName(name):
    ### regex (@"^[A-z][A-z|\.|\s]+$");

Is This A Good Question/Topic? 0
  • +

Replies To: validation of entered of user name

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: validation of entered of user name

Posted 18 May 2012 - 06:44 AM

So, you need a python regex example?

IF you're asking about structure, something like this would work fine:
if isValidName(userName):
    #do something
else:
    #do something else


This post has been edited by atraub: 18 May 2012 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 jone kim  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 07-January 10

Re: validation of entered of user name

Posted 18 May 2012 - 08:18 AM

View Postatraub, on 18 May 2012 - 06:44 AM, said:

So, you need a python regex example?

IF you're asking about structure, something like this would work fine:
if isValidName(userName):
    #do something
else:
    #do something else


I've already seen the regular expressions in python. But I can not understand that. will you provide me the code for my query?
Was This Post Helpful? 0
  • +
  • -

#4 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: validation of entered of user name

Posted 18 May 2012 - 10:38 AM

re.search returns a match object if a match is found and returns None if a match is not found. So, here's what looks right to me:

import re

def isValidUsername(username):
    return re.search("^[A-z][A-z|\.|\s]+$",username) != None



I'm not much of a regular expression guy, so let me know if it doesn't work.
Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: validation of entered of user name

Posted 19 May 2012 - 06:59 PM

View Postjone kim, on 18 May 2012 - 02:51 AM, said:

I've to validate the entered user name but I do not know how to do it.regex might be like this Regex(@"^[A-z][A-z|\.|\s]+$"); but I do not know how to implement regular expression in python via a class.
class name:
def __int__(name):
		self.name = name
	def validateName(name):
    ### regex (@"^[A-z][A-z|\.|\s]+$");


What do you mean by "via a class"? You mean if there is a class, object, or module that will do this for you? Then yes, the re module will give you the functionality you want. re.search() or re.match() will do what you want. Simply put regex in the quotes. One suggestion that I'd make on atraub's example would be to always use raw strings with regex.


import re

def isValidUsername(username):
    return re.search(r'^[A-z][A-z|\.|\s]+$',username) != None


Was This Post Helpful? 1
  • +
  • -

#6 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: validation of entered of user name

Posted 19 May 2012 - 07:50 PM

ahh yes, good call Dogstopper.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1