skip to Main Content

Im some weeks now im trying to make my own validators for my mobile app ,the thing is that sometimes i get a feedback from my users that they cut them off and they are getting errors , im trying to find a solution everywhere but at least in frontend for flutter i cant find any reliable package … many of them are outdated at least 2-3 years.

Also i dont know what to allow and what not in password-email fields , do i need to cut off symbols or characters that can messed up my database or mysql2 can handle it ?

Does anyone have any solution for this or you guys use validators in backend only?

Thanks .
Here is my code :

bool isValidUsername(String username) {
  final RegExp usernamePattern =
      RegExp(r'^[p{L}p{N}_.-]{3,20}$', unicode: true);
  return usernamePattern.hasMatch(username);
}

bool isValidEmail(String email) {
  final RegExp emailPattern = RegExp(
    r'^[^s@]+@[^s@]+.[^s@]+$',
    unicode: true,
  );
  return emailPattern.hasMatch(email);
}

bool isValidPassword(String password) {
  final RegExp passwordPattern = RegExp(
    r'^[p{L}p{N}p{P}p{S}p{Zs}]{8,50}$',
    unicode: true,
  );

  final RegExp problematicPattern = RegExp(
    r'[x00-x1Fx7F]',
    unicode: true,
  );

  return passwordPattern.hasMatch(password) &&
      !problematicPattern.hasMatch(password);
}

2

Answers


  1. Your email validation regex should be replaced with package:email_valid. A regex to match a valid email is around 2400 characters.

    Also, if you practice proper placeholders for your SQL interactions, it should not matter at all what goes into your passwords. Stop filtering those. Especially review Correct Horse Battery Staple and permit anything the user wants, as long as there are enough bits.

    Login or Signup to reply.
  2. I am utilizing the following class for validation purposes. This class is designed to be highly versatile and can be integrated into any application you develop. You have the flexibility to extend it by adding additional validation logic as required. Also you can customize it to meet your specific needs.

    class TValidator{
      TValidator._();
    
      static String? validateEmptyText(String? fieldName, String? value) {
        if (value == null || value.isEmpty) {
          return '$fieldName is required';
        }
        return null;
      }
    
      static String? validateEmail(String? value) {
        if (value == null || value.isEmpty) {
          return 'Email is required.';
        }
    
        // Regular expression for email validation
        final emailRegExp = RegExp(r'^[w-.]+@([w-]+.)+[w-]{2,4}$');
    
        if (!emailRegExp.hasMatch(value)) {
          return 'Invalid email address.';
        }
        return null;
      }
    
      static String? validatePassword(String? value) {
        if (value == null || value.isEmpty) {
          return 'Password is required.';
        }
    
        // Check for minimum password length
        if (value.length < 6) {
          return 'Password must be at least 6 characters long.';
        }
    
        // Check for uppercase letters
        if (!value.contains(RegExp(r'[A-Z]'))) {
          return 'Password must contain at least one uppercase letter.';
        }
    
        // Check for special characters
        if (!value.contains(RegExp(r'[!@#$%^&*(),.?":{}|<>]'))) {
          return 'Password must contain at least one special character.';
        }
    
        return null;
      }
    
      static String? validatePhoneNumber(String? value) {
        if (value == null || value.isEmpty) {
          return 'Phone number is required.';
        }
    
        // Regular expression for phone number validation ( assuming a 10-digit US phone number format )
        final phoneRegExp = RegExp(r'^d{10}$');
    
        if (!phoneRegExp.hasMatch(value)) {
          return 'Invalid phone number format (10-digits required).';
        }
    
        return null;
      }
    
      // Add more custom validators as needed for your specific requirements.
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search