Open In App

Flutter – String Validator

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

If you are a developer and making applications and websites then you must have to validate different things whether it is email, URL, Credit Card Number, divisibility, and many more. If you get the same issue in Flutter to write different functions for validation then now you can use this package in which you will get a number of functions from where you easily validate your string. If it is in a different project you have to write the function again. 

We mostly use its IsEmail function because almost in every app we are asking for an email. We just have to check whether the user has input the proper email or not. So now let’s get started on how you can use this package and its different functions in very simple steps.

Implementation

Add this package to your project:

dependencies:
  string_validator: ^1.0.0

Use it is a different function like this:

For verifying email, isLength, isUrl

Dart




import 'package:flutter/material.dart';
import 'package:string_validator/string_validator.dart';
  
void main() {
    
  String userInput = 'http://localhost:61500this is an invalid url!!!!';
  bool isValid = isURL(userInput);
  print(isValid);
    
  userInput = "https://www.geeksforgeeks.org/";
  isValid = isURL(userInput);
  print(isValid);
  
  userInput = 'me@example.com';
  isValid = isEmail(userInput);
  print(isValid);
    
  userInput = 'geeksforgeeks';
  isValid = isEmail(userInput);
  print(isValid);
    
  userInput = 'password1';
  isValid = isLength(userInput, 12);
  print(isValid);
    
  userInput = 'geeksforgeeks';
  isValid = isLength(userInput, 13);
  print(isValid);
    
  userInput = "12";
  isValid = isDivisibleBy(userInput, 5);
  print(isValid);
    
  isValid = isDivisibleBy(userInput, 4);
  print(isValid);
}


Output:

false
true
true
false
false
true
false
true

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads