59 lines
1.9 KiB
Plaintext
59 lines
1.9 KiB
Plaintext
@page "/phonevalidator"
|
|
|
|
@using System.Text.RegularExpressions
|
|
|
|
|
|
<EditForm Model="@phoneNumberModel" OnValidSubmit="HandleValidSubmit">
|
|
<DataAnnotationsValidator />
|
|
<ValidationSummary />
|
|
|
|
<DxTextBox @bind-Text="phoneNumberModel.PhoneNumber" Placeholder="Enter phone number" />
|
|
<DxButton Context="BtnContext" SubmitFormOnClick="true" RenderStyle=@ButtonRenderStyle.Primary>Validate</DxButton>
|
|
</EditForm>
|
|
|
|
<p>@result</p>
|
|
|
|
@if (validationResult.HasValue)
|
|
{
|
|
<DxLabel CssClass="mt-3">
|
|
@if (validationResult.Value)
|
|
{
|
|
<span class="text-success">The phone number is valid.</span>
|
|
}
|
|
else
|
|
{
|
|
<span class="text-danger">The phone number is invalid.</span>
|
|
}
|
|
</DxLabel>
|
|
}
|
|
|
|
@code {
|
|
private PhoneNumberModel phoneNumberModel = new PhoneNumberModel();
|
|
private bool? validationResult = null;
|
|
private string result = "";
|
|
private void HandleValidSubmit()
|
|
{
|
|
validationResult = IsValidInternationalPhoneNumber(phoneNumberModel.PhoneNumber);
|
|
result = $"Validation result for {phoneNumberModel.PhoneNumber}: {validationResult}";
|
|
}
|
|
|
|
private bool IsValidInternationalPhoneNumber(string phoneNumber)
|
|
{
|
|
if (string.IsNullOrEmpty(phoneNumber))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// string pattern = @"\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*\d\W*(\d{1,2})";
|
|
string pattern = @"^\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\W*\d(\W*\d){1,14}$";
|
|
Regex regex = new Regex(pattern);
|
|
|
|
return regex.IsMatch(phoneNumber);
|
|
}
|
|
|
|
public class PhoneNumberModel
|
|
{
|
|
public string PhoneNumber { get; set; }
|
|
}
|
|
}
|