Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
•
IT NachrichtenWhy is your laptop fan so loud?(22.09.2026 um 02:00 Uhr)
•
IT NachrichtenWhy it's important to unplug your PC during a power outage(22.09.2026 um 02:30 Uhr)
••
IT NachrichtenA cut cable disrupted hundreds of flights across the US(22.09.2026 um 01:48 Uhr)
•••••••
IT NachrichtenWhy is your laptop fan so loud?(22.09.2026 um 02:00 Uhr)
•
IT NachrichtenWhy it's important to unplug your PC during a power outage(22.09.2026 um 02:30 Uhr)
••
IT NachrichtenA cut cable disrupted hundreds of flights across the US(22.09.2026 um 01:48 Uhr)
••••••
Intelligence View
⚡ tsecurity.de Intelligence

Extending String for Validation in Dart 3

In Dart 3, one of the powerful features you can leverage is extensions. Extensions allow you to add new functionality to existing libraries. In this article, we will demonstrate how to extend the String class to include common validation…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In Dart 3, one of the powerful features you can leverage is extensions. Extensions allow you to add new functionality to existing libraries. In this article, we will demonstrate how to extend the String class to include common validation checks such as email validation, numeric check, and phone number validation.






Creating the String Extension



Let's start by creating an extension on the String class. We will define three validation methods:





  1. isValidEmail - Checks if the string is a valid email format.


  2. isNumeric - Checks if the string consists only of numeric characters.


  3. isPhoneNumber - Checks if the string is a valid phone number.



Here's the code for the StringValidation extension:




extension StringValidation on String {
bool get isValidEmail {
final regex = RegExp(r'^[^@]+@[^@]+\.[^@]+');
return regex.hasMatch(this);
}

bool get isNumeric {
final regex = RegExp(r'^-?[0-9]+$');
return regex.hasMatch(this);
}

bool get isPhoneNumber {
final regex = RegExp(r'^\+?[0-9]{10,13}$');
return regex.hasMatch(this);
}
}









Explanation





  • isValidEmail: This method uses a regular expression to check if the string follows a basic email pattern (e.g., [email protected]).


  • isNumeric: This method checks if the string contains only numeric characters, including an optional leading minus sign for negative numbers.


  • isPhoneNumber: This method validates if the string is a phone number. It allows an optional leading plus sign and checks for a length between 10 to 13 digits.






Usage



Using these extensions is straightforward. You simply call the new methods on any string instance. Below is an example demonstrating how to use these validation methods:




void main() {
var email = "[email protected]";
var phone = "+1234567890";
var number = "12345";

print(email.isValidEmail); // Output: true
print(phone.isPhoneNumber); // Output: true
print(number.isNumeric); // Output: true
}









Explanation





  • email.isValidEmail: Checks if the email string is a valid email. The output will be true if the string matches the email pattern.


  • phone.isPhoneNumber: Validates if the phone string is a valid phone number. The output will be true if the string matches the phone number pattern.


  • number.isNumeric: Checks if the number string consists of numeric characters. The output will be true if the string matches the numeric pattern.






Conclusion



By using Dart's extension methods, you can add reusable validation logic to the String class. This approach makes your code cleaner and more modular. You can extend this pattern to include more validations as needed, enhancing the robustness of your applications.



Try integrating these extensions into your Dart projects and see how they can simplify your validation logic. Happy coding!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Extending String for Validation in Dart 3

Thematisch verwandte Begriffe: Extending, String, Validation, Dart · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-49449 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel • Rechts: nächster Artikel • unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger • Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick