Home > Software design >  how to modify a String from a Textfield before validated using validator?
how to modify a String from a Textfield before validated using validator?

Time:09-21

I am using form field validator, I think it will be the same if using normal TextField, and I have form field like this

  TextFormField(  
    onChanged: (val) => selectedEmail = val,  
    validator: EmailValidator(errorText: "Email is not valid"),  
  )  

unfortunately, my user sometimes unintentionally will put an empty string at the end of email string like this :

"[email protected] "

as you can see, I have email validator here, but the email validator will consider the string with empty space like that as an invalid email.

I want to remove or trim the email string first before it is validated by the EmailValidator, how to do that?

CodePudding user response:

  TextFormField(  
    onChanged: (val) => selectedEmail = val,  
    validator: EmailValidator(errorText: "Email is not valid"),  
        inputFormatters: [FilteringTextInputFormatter.deny(RegExp(r'\s'))]

  ) 

FilteringTextInputFormatter.deny(RegExp(r'\s')) it's deny white space in the TextFormField

CodePudding user response:

It could be helpful.

TextFormField(  
    onChanged: (val) => selectedEmail.trim() = val.trim(),  
    validator: EmailValidator(errorText: "Email is not valid"),  
  )  
  • Related