Hi @打玻璃
But If it trigger one time then you go on enter something it will trigger everytime you enter one character
For this problem, this is by design, so that it is more clearly to let the user knows whether the input value is valid or not.
You can imagine that: if it is designed to use the change event (like this first time), the user needs to do another action/step to make the textbox lose focus, after that validation will work. But if use this method (trigger every time you enter one character), we could easier to know whether the new value is valid or not.
If you still want to use the change event to validate the new data, refer to the following sample: use JQuery Aajx to call the handler method and valid the data by yourself, then based on the validate result to show the error message:
.csthml: check the Email2
@page
@model RazorAspNet6Sample.Pages.RemoteValidationModel
<form method="post">
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" />
<span asp-validation-for="Email"></span><br>
</div>
<div class="form-group">
<label asp-for="Email2" class="control-label"></label>
<input asp-for="Email2" />
<span id="validateresult" style="color:red"></span><br>
</div>
<input type="submit" />
</form>
@section scripts{
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<partial name="_ValidationScriptsPartial" />
<script src="~/lib/jquery-ajax-unobtrusive/dist/jquery.unobtrusive-ajax.min.js"></script>
<script>
$(function(){
$("input[name='Email2']").on("change",function(){
event.preventDefault();
var mail = $(this).val();
$.ajax({
type: 'POST',
url: "/RemoteValidation/?handler=CheckEmail2",
data: {"email":mail},
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
success: function (response) {
$("#validateresult").html("");
if(response.status == false){
$("#validateresult").html(response.message)
}
},
failure: function (error) {
alert(error);
}
});
});
})
</script>
}
.cshtml.cs
public class RemoteValidationModel : PageModel
{
[PageRemote(
ErrorMessage = "!!! Duplicate Email Address !!!",
AdditionalFields = "__RequestVerificationToken",
HttpMethod = "post",
PageHandler = "CheckEmail"
)]
[BindProperty]
public string Email { get; set; }
[BindProperty]
public string Email2 { get; set; }
public void OnGet()
{
}
public JsonResult OnPostCheckEmail()
{
var existingEmails = new[] { "******@test.com", "******@test.com", "******@test.com" };
var valid = !existingEmails.Contains(Email);
return new JsonResult(valid);
}
public JsonResult OnPostCheckEmail2(string email)
{
var existingEmails = new[] { "******@test.com", "******@test.com", "******@test.com" };
var valid = !existingEmails.Contains(email);
if (valid)
{
return new JsonResult(new { status = true, message = "Avaiable Email Address" });
}
return new JsonResult(new { status = false, message = "!!! Duplicate Email Address !!!" });
}
}
The result as below: as we can see, it will trigger the validation method after the value changed.

Best regards,
Dillion