简体   繁体   中英

how to modify a String from a Textfield before validated using validator?

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:

"john@gmail.com "

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?

  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

It could be helpful.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM