xuantruongbn
Gà con
Chào anh em cộng đồng lại mong đc hỗ trợ từ anh em. mình có 1 calss bên dưới giờ khai báo mảng string và duyệt qua mảng foreach như nào cho code gọn nhất đc anh em.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FormsApp2.Class
{
public static class ClassValidateProvider
{
//Ten user khong co khoang trang tu 6-20 ky tu
public static Regex UserValidation()
{
// string pattern = "^.*[a-z][0-9]{4,}$";
string pattern = "(?=^.{6,20}$)([A-Za-z]{1})([A-Za-z0-9!@#$%_\\^\\&\\*\\-\\.\\?]{5,19})$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_user = UserValidation();
//Tao mat khau 8-15 ky tu bao gom ky tu, so va it nhat 1 ky tu hoa
public static Regex PasswordValidation()
{
string pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_password = PasswordValidation();
//Tao dia chi Mail
public static Regex MailValidation()
{
string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_mail = MailValidation();
//Full Name
public static Regex FullNameValidation()
{
//string pattern = "^.*[a-zA-Z]{4,}$";
string pattern = "[a-zA-Z]*( [a-zA-Z]*)?";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_fullname = FullNameValidation();
//Nhap so dien thoai
public static Regex PhoneValidation()
{
string pattern = "^\\(?([0-9]{3})\\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_phone = PhoneValidation();
//Nhap so
public static Regex IntegerValidation()
{
string pattern = "^[-+]?[0-9]*\\.?[0-9]+$";
return new Regex(pattern);
}
public static Regex vaildate_integer = IntegerValidation();
// TCP port numbers from 0 to 65535
public static Regex SogioihanValidation()
{
// string pattern = "^([0-5]?\\d?\\d?\\d?\\d|6[0-4]\\d\\d\\d|65[0-4]\\d\\d|655[0-2]\\d|6553[0-5])$";
string pattern = "^(0|([1-9]\\d{0,3}|[1-5]\\d{4}|[6][0-5][0-5]([0-2]\\d|[3][0-5])))$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_sogioihan = SogioihanValidation();
//IPv4 (192.168.0.1) regex
public static Regex IPv4Validation()
{
string pattern = "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
return new Regex(pattern);
}
public static Regex vaildate_ipv4 = IPv4Validation();
//IPv6 regex
public static Regex IPv6Validation()
{
string pattern = "^(([0-9a-fA-F]{1,4}{7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}{1,7}:|([0-9a-fA-F]{1,4}{1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}{1,5}[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}{1,4}[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}{1,3}[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}{1,2}[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}[0-9a-fA-F]{1,4}){1,6})|[0-9a-fA-F]{1,4}){1,7}||fe80:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|:ffff0{1,4}){0,1}{0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}{1,4}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
return new Regex(pattern);
}
public static Regex vaildate_ipv6 = IPv6Validation();
//Simple date regex (DD/MM/YYYY)
public static Regex NgaythangnamValidation()
{
string pattern = "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_ngaythangnam = NgaythangnamValidation();
//URL regex that starts with HTTP or HTTPS
public static Regex UrlValidation()
{
string pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_url = UrlValidation();
//ZIP code regex
public static Regex ZipcodeValidation()
{
string pattern = "^[0-9]{5}(?:-[0-9]{4})?$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_zipcode = ZipcodeValidation();
//UUID regex
public static Regex UuidValidation()
{
string pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_uuid = UuidValidation();
//Nhap chuoi string
public static Regex StringValidation()
{
string pattern = "^[a-zA-Z]+$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_string = StringValidation();
//MAC Address
public static Regex MacAdressValidation()
{
string pattern = "^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_macadress = MacAdressValidation();
//Alpha Numeric
public static Regex AlphanumericValidation()
{
string pattern = "^[a-zA-Z0-9]+$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_alphanumeric = AlphanumericValidation();
//between so nam trong khoang (vd:tuoi tu 0 den 120)
public static Regex NumericbeteenValidation()
{
string pattern = "^[0-120]$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_numericbetween = NumericbeteenValidation();
//Active URL
public static Regex ActiveURLValidation()
{
string pattern = "https://laravel.com/docs/8.x/validation#available-validation-rules";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_activeurln = ActiveURLValidation();
//Alpha dash (dau gach ngang)VD: 4D28C5AD-6482-41CD-B84E-4573F384BB5C
public static Regex DashValidation()
{
string pattern = "^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_dash = DashValidation();
//boolean (true or false)
public static Regex BooleanValidation()
{
string pattern = "^([Vv]+(erdade(iro)?)?|[Ff]+(als[eo])?|[Tt]+(rue)?|0|[\\+\\-]?1)$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_boolean = BooleanValidation();
//Dimensions (Image Files)
public static Regex FileimageValidation()
{
string pattern = "-[0-9]*[x][0-9]*";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_fileimage = FileimageValidation();
//[email protected],end with:A short and sweet email address validator. Checks that the username starts and ends with an alphanumeric character, allows a few non-repeating 'special characters' (namely -, ., _, +, &
public static Regex EndwithValidation()
{
string pattern = "^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_endwith = EndwithValidation();
//enum:dd/MM/yyyy 01/01/1900 through 31/12/2099
public static Regex EnumValidation()
{
string pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_enum = EnumValidation();
//Credit card: checks that the format is 16 digits: 1234-1234-1234-1234 | 1234123412341234
public static Regex CreditcardValidation()
{
string pattern = "\\d{4}-?\\d{4}-?\\d{4}-?\\d{4}";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_creditcard = CreditcardValidation();
//Validates MM/YY for rough checking credit card expiration dates: 11/20 | 01/28
public static Regex MonthyearexpireValidation()
{
string pattern = "^((0[1-9])|(1[0-2]))\\/(\\d{2})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_monthyearexpire = MonthyearexpireValidation();
//gioi han ky tu nhap vao, vd: ky tu nhap vao nam trong khoang tu 4-8
public static Regex GioihankytuValidation()
{
string pattern = "^.{4,8}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_gioihankytu = GioihankytuValidation();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FormsApp2.Class
{
public static class ClassValidateProvider
{
//Ten user khong co khoang trang tu 6-20 ky tu
public static Regex UserValidation()
{
// string pattern = "^.*[a-z][0-9]{4,}$";
string pattern = "(?=^.{6,20}$)([A-Za-z]{1})([A-Za-z0-9!@#$%_\\^\\&\\*\\-\\.\\?]{5,19})$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_user = UserValidation();
//Tao mat khau 8-15 ky tu bao gom ky tu, so va it nhat 1 ky tu hoa
public static Regex PasswordValidation()
{
string pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,15})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_password = PasswordValidation();
//Tao dia chi Mail
public static Regex MailValidation()
{
string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
+ @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
+ @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_mail = MailValidation();
//Full Name
public static Regex FullNameValidation()
{
//string pattern = "^.*[a-zA-Z]{4,}$";
string pattern = "[a-zA-Z]*( [a-zA-Z]*)?";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_fullname = FullNameValidation();
//Nhap so dien thoai
public static Regex PhoneValidation()
{
string pattern = "^\\(?([0-9]{3})\\)?[-.●]?([0-9]{3})[-.●]?([0-9]{4})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_phone = PhoneValidation();
//Nhap so
public static Regex IntegerValidation()
{
string pattern = "^[-+]?[0-9]*\\.?[0-9]+$";
return new Regex(pattern);
}
public static Regex vaildate_integer = IntegerValidation();
// TCP port numbers from 0 to 65535
public static Regex SogioihanValidation()
{
// string pattern = "^([0-5]?\\d?\\d?\\d?\\d|6[0-4]\\d\\d\\d|65[0-4]\\d\\d|655[0-2]\\d|6553[0-5])$";
string pattern = "^(0|([1-9]\\d{0,3}|[1-5]\\d{4}|[6][0-5][0-5]([0-2]\\d|[3][0-5])))$";
return new Regex(pattern,RegexOptions.IgnoreCase);
}
public static Regex vaildate_sogioihan = SogioihanValidation();
//IPv4 (192.168.0.1) regex
public static Regex IPv4Validation()
{
string pattern = "^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
return new Regex(pattern);
}
public static Regex vaildate_ipv4 = IPv4Validation();
//IPv6 regex
public static Regex IPv6Validation()
{
string pattern = "^(([0-9a-fA-F]{1,4}{7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}{1,7}:|([0-9a-fA-F]{1,4}{1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}{1,5}[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}{1,4}[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}{1,3}[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}{1,2}[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}[0-9a-fA-F]{1,4}){1,6})|[0-9a-fA-F]{1,4}){1,7}||fe80:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|:ffff0{1,4}){0,1}{0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}{1,4}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$";
return new Regex(pattern);
}
public static Regex vaildate_ipv6 = IPv6Validation();
//Simple date regex (DD/MM/YYYY)
public static Regex NgaythangnamValidation()
{
string pattern = "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_ngaythangnam = NgaythangnamValidation();
//URL regex that starts with HTTP or HTTPS
public static Regex UrlValidation()
{
string pattern = "^https?:\\/\\/(?:www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b(?:[-a-zA-Z0-9()@:%_\\+.~#?&\\/=]*)$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_url = UrlValidation();
//ZIP code regex
public static Regex ZipcodeValidation()
{
string pattern = "^[0-9]{5}(?:-[0-9]{4})?$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_zipcode = ZipcodeValidation();
//UUID regex
public static Regex UuidValidation()
{
string pattern = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_uuid = UuidValidation();
//Nhap chuoi string
public static Regex StringValidation()
{
string pattern = "^[a-zA-Z]+$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_string = StringValidation();
//MAC Address
public static Regex MacAdressValidation()
{
string pattern = "^(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_macadress = MacAdressValidation();
//Alpha Numeric
public static Regex AlphanumericValidation()
{
string pattern = "^[a-zA-Z0-9]+$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_alphanumeric = AlphanumericValidation();
//between so nam trong khoang (vd:tuoi tu 0 den 120)
public static Regex NumericbeteenValidation()
{
string pattern = "^[0-120]$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_numericbetween = NumericbeteenValidation();
//Active URL
public static Regex ActiveURLValidation()
{
string pattern = "https://laravel.com/docs/8.x/validation#available-validation-rules";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_activeurln = ActiveURLValidation();
//Alpha dash (dau gach ngang)VD: 4D28C5AD-6482-41CD-B84E-4573F384BB5C
public static Regex DashValidation()
{
string pattern = "^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_dash = DashValidation();
//boolean (true or false)
public static Regex BooleanValidation()
{
string pattern = "^([Vv]+(erdade(iro)?)?|[Ff]+(als[eo])?|[Tt]+(rue)?|0|[\\+\\-]?1)$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_boolean = BooleanValidation();
//Dimensions (Image Files)
public static Regex FileimageValidation()
{
string pattern = "-[0-9]*[x][0-9]*";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_fileimage = FileimageValidation();
//[email protected],end with:A short and sweet email address validator. Checks that the username starts and ends with an alphanumeric character, allows a few non-repeating 'special characters' (namely -, ., _, +, &
public static Regex EndwithValidation()
{
string pattern = "^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_endwith = EndwithValidation();
//enum:dd/MM/yyyy 01/01/1900 through 31/12/2099
public static Regex EnumValidation()
{
string pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_enum = EnumValidation();
//Credit card: checks that the format is 16 digits: 1234-1234-1234-1234 | 1234123412341234
public static Regex CreditcardValidation()
{
string pattern = "\\d{4}-?\\d{4}-?\\d{4}-?\\d{4}";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_creditcard = CreditcardValidation();
//Validates MM/YY for rough checking credit card expiration dates: 11/20 | 01/28
public static Regex MonthyearexpireValidation()
{
string pattern = "^((0[1-9])|(1[0-2]))\\/(\\d{2})$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_monthyearexpire = MonthyearexpireValidation();
//gioi han ky tu nhap vao, vd: ky tu nhap vao nam trong khoang tu 4-8
public static Regex GioihankytuValidation()
{
string pattern = "^.{4,8}$";
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public static Regex vaildate_gioihankytu = GioihankytuValidation();
}
}