At least one valid text field out of multiple text fields

72 Views Asked by At

Suppose I have two input text fields: one for a regular phone number and another for a cellphone number. How would I go about validating that at least one field is not empty?

E.g. if telephone is not empty but cellphone is empty, it's considered valid. if cellphone is not empty but telephone is empty, it's considered valid. if both telephone and cellphone are empty, it's considered invalid.

Is it possible for this to be expressed with FormEncode?

2

There are 2 best solutions below

0
On

To validate with formencode that at least one field is filled.

class RequireNumber(formencode.Schema):
    phone_number = formencode.validators.PhoneNumber(if_missing=None)
    cell_number = formencode.validators.PhoneNumber(if_missing=None)
    chained_validators = [formencode.validators.RequireIfMissing('phone_number', missing='cell_number')]
    chained_validators = [formencode.validators.RequireIfMissing('cell_number', missing='phone_number')]
2
On

use document.GetElementByID to get the value in the input text

if reqularPhoneNumber != "" or cellPhoneNumber != "":
    # Do your stuff    

or - is the logical or operator, the if is evaluated as true if either of the conditions is true

EDIT: Changed || to or as with Python