Select to view content in your preferred language

How can I check if a string has numbers (0-9) and dashes?

1028
9
Jump to solution
08-06-2013 07:02 AM
ionarawilson1
Deactivated User
How can I check if a string has numbers (0-9) and dashes? The user will type a phone number, but I want to make sure he doesn't enter letters, just numbers and dashes. I already wrote code to make sure it is the right size (12 characters) and the dashes are in the right order, but I want to restrict the string to have no letters. Thank you!!!
    def updateMessages(self, parameters):         """Modify the messages created by internal validation for each tool         parameter.  This method is called after internal validation."""         Status = parameters[0]         #this can only be included if the field is not optional:         Phone = parameters[12]         if Phone.value:             Phonetxt = Phone.value             if len(Phonetxt) < 12:                 Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")             if len(Phonetxt) > 12:                 Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                   if not "-" in Phonetxt:                 Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")              if len(Phonetxt) == 12:                 phone3dig = Phonetxt[0:4]                 if not "-" in phone3dig:                     Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                 phone32dig = Phonetxt[0:3]                 if "-" in phone32dig:                     Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                 phone6dig = Phonetxt[4:8]                 if not "-" in phone6dig:                     Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                 phone62dig = Phonetxt[4:7]                 if "-" in phone62dig:                     Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                 phone12dig = Phonetxt[8:]                 if "-" in phone12dig:                     Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")                
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
    def updateMessages(self, parameters):         import re         phonenumber_format = re.compile("\d{3}-\d{3}-\d{4}")         Phone = parameters[12]         if Phone.value:            not phonenumber_format.match(Phone.value):                 Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")

View solution in original post

0 Kudos
9 Replies
JasonScheirer
Esri Alum
    def updateMessages(self, parameters):         import re         phonenumber_format = re.compile("\d{3}-\d{3}-\d{4}")         Phone = parameters[12]         if Phone.value:            not phonenumber_format.match(Phone.value):                 Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")
0 Kudos
T__WayneWhitley
Honored Contributor
This may be clumsy, but 1 way to get the job done:

...load a list 0-9:
>>> allowed = []
>>> for i in range(10):
 allowed.append(str(i))

 
>>> print allowed
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


...also, load the hyphen:
>>> allowed.append('-')
>>> print allowed
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-']


...then, let's say you have a phone number, inadvertently has an 'e' in it, then it must pass the following test:
>>> txt = '4e6-234-2345'
>>> for each in txt:
 if each in allowed:
  pass
 else:
  print 'whoa...' + each + ' not allowed'

  
whoa...e not allowed
>>> 


...additionally, if you need to test for more than 1 hyphen:
>>> pos1 = txt.find('-')
>>> if txt.find('-', pos1 + 1):
 print 'wait a min, you have a 2nd hyphen!'

 
wait a min, you have a 2nd hyphen!
>>>
0 Kudos
ChristopherBlinn1
Deactivated User
    def updateMessages(self, parameters):
        import re
        phonenumber_format = re.compile("\d{3}-\d{3}-\d{4}")
        Phone = parameters[12]
        if Phone.value:
           not phonenumber_format.match(Phone.value):
                Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")


This is close to what I was thinking, but \d will allow for ANY digit character, and many exist outside of [0-9].

The above will make sure you are getting XXX-XXX-XXXX where X is a digit, but if someone puts in an Eastern Arabic decimal, it too will be allowed.

You should create a list of values and check against them to make sure only wanted digits are used.

Then use both the list and Jason's response for best results.
0 Kudos
JasonScheirer
Esri Alum
No, \d requires the re.U flag to match anything outside of 0-9, per the documentation:


[INDENT]\d
[INDENT]When the UNICODE flag is not specified, matches any decimal digit; this is equivalent to the set [0-9]. With UNICODE, it will match whatever is classified as a decimal digit in the Unicode character properties database.[/INDENT][/INDENT]
0 Kudos
ChristopherBlinn1
Deactivated User
No, \d requires the re.U flag to match anything outside of 0-9, per the documentation:


[INDENT]\d
[INDENT]When the UNICODE flag is not specified, matches any decimal digit; this is equivalent to the set [0-9]. With UNICODE, it will match whatever is classified as a decimal digit in the Unicode character properties database.[/INDENT][/INDENT]


Looks like we all learned something today.  Thanks for the clarification.
0 Kudos
T__WayneWhitley
Honored Contributor
I don't know, to be safe you may want to use an explicit list/dict, but this statement from the python docs may help:

\d
When the UNICODE flag is not specified, matches any decimal digit; this is equivalent to the set [0-9]. With UNICODE, it will match whatever is classified as a digit in the Unicode character properties database.

For example, the syntax to expand the def to allow Unicode characters is to use a 'flag' param (optional parameter) as follows:
re.compile("\d{3}-\d{3}-d{4}", flags=re.UNICODE)

...but if I'm not mistaken, ASCII is the default?   I'm not 100% but wouldn't that remove from consideration, say, an Eastern Arabic decimal? 

http://docs.python.org/2.6/library/re.html?#re.compile
http://docs.python.org/2.6/library/re.html?#re.UNICODE

Hope that helps - I have little experience with the 'regular expressions' re module, very interesting.

Enjoy,
Wayne


Haha, great stuff -- looks like while my head was spinning and forming my response, the guys have already responded.  Way to go, Jason and Christopher!  Thanks for this - I've been struggling dealing with Unicode with something else I've been tinkering with, perhaps a use for re - but for now I've gotten around it with importing codecs and opening a problematic txt file with encoding='utf-16-le'.

...after deleting my previous post, I probably wouldn't have given this a 2nd look, but thanks to Christopher questioning what exactly \d specifies, I agree wholeheartedly:

"Looks like we all learned something today."
0 Kudos
ionarawilson1
Deactivated User
Thank you so much Jason, this worked for me. I've just added "if" to the second if statement and it worked wonders!
Thank you Wayne and Christoper for your comments also! I've never heard of the re module so this will be very helpful for expressions in the future!!!:cool:

import re
        phonenumber_format = re.compile("\d{3}-\d{3}-\d{4}")
        if Phone.value:
             if not phonenumber_format.match(Phone.value):
                    Phone.setErrorMessage("Make sure the phone number is in this format: ex: 979-458-6630")
             
0 Kudos
NeilAyres
MVP Alum
A snippet from Mark Pilgrim's "Dive into Python", chapter 7 about the re module..
"Regular expressions are extremely powerful, but they are not the correct solution for every problem. You should learn
enough about them to know when they are appropriate, when they will solve your problems, and when they will cause
more problems than they solve.
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
Now they have two problems.
�??�??Jamie Zawinski, in comp.emacs.xemacs
(http://groups.google.com/groups?selm=33F0C496.370D7C45%40netscape.com)
"

All the stuff about parsing phone numbers etc is in there. But re expressions just give me a headache...!
But well done for the answer.
0 Kudos
ionarawilson1
Deactivated User
I agree Neil, expressions are a solution but also another problem! It is like opening a can of worms 🙂
0 Kudos