Skip to main content

Form Validation

Validate user input in forms with comprehensive checks for emails, passwords, and phone numbers.
import 'package:localregex/localregex.dart';

class RegistrationForm {
  String? validateEmail(String? value) {
    if (value == null || value.isEmpty) {
      return 'Email is required';
    }
    if (!LocalRegex.isEmail(value)) {
      return 'Please enter a valid email address';
    }
    return null;
  }

  String? validatePassword(String? value) {
    if (value == null || value.isEmpty) {
      return 'Password is required';
    }
    if (!LocalRegex.isPassword(value)) {
      return 'Password must contain at least 8 characters, '
          'including uppercase, lowercase, number and special character';
    }
    return null;
  }

  String? validatePhone(String? value) {
    if (value == null || value.isEmpty) {
      return 'Phone number is required';
    }
    if (!LocalRegex.isZimMobile(value)) {
      return 'Please enter a valid Zimbabwean mobile number';
    }
    return null;
  }

  // Generate a secure password for users
  String generateSecurePassword() {
    return LocalRegex.generatePassword(length: 16);
  }
}

Phone Number Formatter

Format phone numbers as users type, supporting multiple display formats.
import 'package:localregex/localregex.dart';

class PhoneNumberFormatter {
  String formatAsUserTypes(String input) {
    try {
      // Format to country code (+263) format
      return input.formatNumber(
        formatType: FormatType.countryCodePlus,
        cleanNumber: true,
      ) ?? input;
    } catch (e) {
      // Return original input if invalid
      return input;
    }
  }

  // Detect network provider
  String detectProvider(String phoneNumber) {
    if (LocalRegex.isEconet(phoneNumber)) {
      return 'Econet';
    } else if (LocalRegex.isNetone(phoneNumber)) {
      return 'NetOne';
    } else if (LocalRegex.isTelecel(phoneNumber)) {
      return 'Telecel';
    } else if (LocalRegex.isZimVoip(phoneNumber)) {
      return 'VoIP';
    }
    return 'Unknown';
  }
}

Identity Verification

Validate multiple document types for identity verification systems.
import 'package:localregex/localregex.dart';

class IDVerification {
  bool verifyNationalID(String idNumber) {
    return LocalRegex.isZimID(idNumber);
  }

  String formatIDForDisplay(String idNumber) {
    try {
      // Format with proper spacing: 63-1234567 A 12
      return idNumber.formatID(
        formatType: IdFormatType.proper,
      )!;
    } catch (e) {
      throw Exception('Invalid national ID: $idNumber');
    }
  }

  String formatIDForStorage(String idNumber) {
    try {
      // Format without spaces: 631234567A12
      return idNumber.formatID(
        formatType: IdFormatType.noSpace,
      )!;
    } catch (e) {
      throw Exception('Invalid national ID: $idNumber');
    }
  }
}

Student Registration

Validate student IDs for different educational institutions.
import 'package:localregex/localregex.dart';

class UniversityRegistration {
  String? validateStudentID(String? value) {
    if (value == null || value.isEmpty) {
      return 'Student ID is required';
    }

    // Validate HIT student ID format (e.g., H180202M)
    if (!LocalSchools.hit.isStudent(value)) {
      return 'Invalid HIT student ID format';
    }

    return null;
  }

  bool registerStudent(String studentId, String email) {
    // Validate student ID
    if (!LocalSchools.hit.isStudent(studentId)) {
      return false;
    }

    // Validate email
    if (!LocalRegex.isEmail(email)) {
      return false;
    }

    // Registration logic here...
    return true;
  }
}

Additional Examples

URL Validation

import 'package:localregex/localregex.dart';

class URLValidator {
  bool isValidWebsite(String url) {
    return LocalRegex.isUrl(url);
  }

  String? validateURLInput(String? value) {
    if (value == null || value.isEmpty) {
      return 'URL is required';
    }
    if (!LocalRegex.isUrl(value)) {
      return 'Please enter a valid URL';
    }
    return null;
  }
}

Date Validation

import 'package:localregex/localregex.dart';

class DateValidator {
  bool isValidDateString(String date) {
    // Validates dates in DD/MM/YYYY or DD-MM-YYYY format
    return LocalRegex.isDate(date);
  }

  String? validateDateOfBirth(String? value) {
    if (value == null || value.isEmpty) {
      return 'Date of birth is required';
    }
    if (!LocalRegex.isDate(value)) {
      return 'Please enter a valid date (DD/MM/YYYY or DD-MM-YYYY)';
    }
    return null;
  }
}

IP Address Validation

import 'package:localregex/localregex.dart';

class NetworkValidator {
  bool isValidIP(String ipAddress) {
    return LocalRegex.isIpAddress(ipAddress);
  }

  String? validateIPAddress(String? value) {
    if (value == null || value.isEmpty) {
      return 'IP address is required';
    }
    if (!LocalRegex.isIpAddress(value)) {
      return 'Please enter a valid IP address';
    }
    return null;
  }
}

Build docs developers (and LLMs) love