0

I have a question about trim function. when I am using trim it's not working. when I am adding white space and then used the trim function but still my validation is true. instead of trim has to remove all white space. Hope you understand the question. thank you in advance.

 Widget _buildUserNameField() {
    return EnsureVisibleWhenFocused(
      focusNode: _emailFocusNode,
      child: TudoEmailWidget(
        focusNode: _emailFocusNode,
        prefixIcon: Icon(Icons.email),
        labelText: AppConstantsValue.appConst['login']['email']['translation'],
        validator: Validators().validateEmail,
        onSaved: (val) => _username = val.trim(),
      ),
    );
  }
Rutvik Gumasana
  • 1,458
  • 11
  • 42
  • 66
  • 1
    `trim()` method only remove the leading and trailing spaces. To remove all spaces, you can use a regex, see https://stackoverflow.com/questions/51508321/how-to-remove-all-whitespace-of-a-string-in-dart – Yann39 Sep 30 '19 at 06:31
  • yes i want to remove only trailing spaces – Rutvik Gumasana Sep 30 '19 at 06:32
  • When i am adding email then i am adding single space then validation is becomes true and it will show an error that invalid email. that's i used trim method to remove space from trail but it won't work – Rutvik Gumasana Sep 30 '19 at 06:33
  • `onSaved` event is called only when you do a `form.save()`. If you want to validate the value before submitting the form, just pass the value to you validator this way : `validator: (val) => Validators().validateEmail(val.trim())` and update your `validateEmail` to accept the parameter. – Yann39 Sep 30 '19 at 06:41
  • no i want just to remove blank space from the trail that's it – Rutvik Gumasana Sep 30 '19 at 06:43
  • class Validators { String validateEmail(String value) { String pattern = r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'; RegExp regExp = new RegExp(pattern); if (value.length == 0) { return "Email is Required"; } else if (!regExp.hasMatch(value)) { return "Invalid Email"; } else { return null; } } } THis is my validator class how can i update as per you suggestion?? – Rutvik Gumasana Sep 30 '19 at 06:47
  • 1
    To remove only trailing spaces use the [trimRight()](https://api.dartlang.org/stable/2.5.1/dart-core/String/trimRight.html) function – Yann39 Sep 30 '19 at 06:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200142/discussion-between-rutvik-gumasana-and-yann39). – Rutvik Gumasana Sep 30 '19 at 06:47
  • But it is not working – Rutvik Gumasana Sep 30 '19 at 06:53

1 Answers1

3

The onSaved event is called only when you save the form (call to form.save()).

If you want to validate the value before submitting the form, just pass the value to you validator this way :

validator: (val) => Validators().validateEmail(val)

and update your validateEmail to accept the parameter.

Also if you want to remove only trailing spaces use the trimRight() function.

As an example, this work perfectly on my side :

class Validators {
  static String validateEmail(String value) {
    String pattern =
        r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
    RegExp regExp = new RegExp(pattern);
    if (value == null || value.length <= 0) {
      return "Email is Required";
    } else if (!regExp.hasMatch(value)) {
      return "Invalid Email";
    } else {
      return null;
    }
  }
}
TextFormField(
  maxLines: 1,
  inputFormatters: [LengthLimitingTextInputFormatter(128)],
  validator: (val) => Validators.validateEmail(val.trimRight()),
  onSaved: (val) => _currMember.email = val,
  initialValue: _currMember.email,
),
Rutvik Gumasana
  • 1,458
  • 11
  • 42
  • 66
Yann39
  • 14,285
  • 11
  • 56
  • 84
  • This RegExp adequately checks for spaces in the supplied (i.e. input) email string. Two thumbs up from me ! – SilSur May 01 '20 at 22:33
  • I think I took it from this community answer : https://stackoverflow.com/a/46181/1274485 – Yann39 May 01 '20 at 22:55