Validating US phone number in WTForms
<p>I'm working with a flask app and am using wtforms:</p>

<pre><code>class RegisterForm(Form):
    username = StringField('Username', validators=[DataRequired(), Length(min=3, max=25)])
    email = StringField('Email', validators=[DataRequired(), Email(), Length(min=6, max=40)])
    phone = StringField('Phone', validators=[DataRequired(), Length(10)])
</code></pre>

<p>I've just added phone and am looking for a way to validate a US number. I came across <a href="http://wtforms-components.readthedocs.org/en/stable/#phonenumberfield" rel="nofollow">http://wtforms-components.readthedocs.org/en/stable/#phonenumberfield</a> but this does not appear to have phonenumberfield any longer. Whats the best way to handle this with wtfforms?</p>

<p>edited class:</p>

<pre><code>class RegisterForm(Form):
    username = StringField('Username', validators=[DataRequired(), Length(min=3, max=25)])
    email = StringField('Email', validators=[DataRequired(), Email(), Length(min=6, max=40)])
    phone = StringField('Phone', validators=[DataRequired(),validate_phone('RegisterForm','phone'), Length(min=6, max=40)])

    def validate_phone(form, field):
        if len(field.data) &gt; 16:
            raise ValidationError('Invalid phone number.')
        try:
            input_number = phonenumbers.parse(field.data)
            if not (phonenumbers.is_valid_number(input_number)):
                raise ValidationError('Invalid phone number.')
        except:
            input_number = phonenumbers.parse("+1"+field.data)
            if not (phonenumbers.is_valid_number(input_number)):
                raise ValidationError('Invalid phone number.')
</code></pre>