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")
Solved! Go to Solution.
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")
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")
>>> allowed = [] >>> for i in range(10): allowed.append(str(i)) >>> print allowed ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> allowed.append('-') >>> print allowed ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-']
>>> txt = '4e6-234-2345' >>> for each in txt: if each in allowed: pass else: print 'whoa...' + each + ' not allowed' whoa...e not allowed >>>
>>> 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! >>>
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")
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]
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")