AnsweredAssumed Answered

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

Question asked by ionarawilson on Aug 6, 2013
Latest reply on Aug 12, 2013 by ionarawilson
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")                

Outcomes