Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

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

$
0
0
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!!!
Code:

    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")


Viewing all articles
Browse latest Browse all 2485

Trending Articles