简体   繁体   English

如何在按钮单击时使用 HTML 表单验证?

[英]How to use HTML form validation on button click?

I'm creating a application using SyncFusion and Blazor.我正在使用 SyncFusion 和 Blazor 创建一个应用程序。 In this application I have a login page which looks like this:在这个应用程序中,我有一个如下所示的登录页面:

在此处输入图像描述

Using this code:使用此代码:

<EditForm Model="@ModelValue">
    <SfTextBox @ref="user" Placeholder="E-mail" Created="@onCreateUser" CssClass="custom-login" Type="InputType.Email" @bind-Value="@ModelValue.Email" required></SfTextBox>


    <SfTextBox @ref="password" Placeholder="Password" Created="@onCreatePassword" CssClass="custom-login" Type="InputType.Password" @bind-Value="@ModelValue.Password" required minlength="7"></SfTextBox>

    <SfCheckBox Label="Stay Signed In" @bind-Checked="rememberUser" CssClass="stay-signed-in-checkbox"></SfCheckBox>
    <SfButton CssClass="forgot-password-button">Forgot Password?</SfButton>

     <SfButton CssClass="login-button" OnClick="validateForm">LOGIN</SfButton>
</EditForm>

@code {
SfTextBox user;
SfTextBox password;
private bool rememberUser;
private User ModelValue = new User();

public void validateForm()
{ 

}

/*ICON*/
public void onCreateUser()
{
    this.user.AddIcon("prepend", "oi oi-person");
}

public void onCreatePassword()
{
    this.password.AddIcon("prepend", "oi oi-key");
}
}

In this form are html browser validation popups applied(automatically).在此表单中(自动)应用 html 浏览器验证弹出窗口。 These work kind of weird in my situation therefore i created a video illustration it properly:在我的情况下,这些工作有点奇怪,因此我正确地创建了一个视频插图:

https://drive.google.com/file/d/1PhETRPkgDag_DHWYlBFJL1qnodxDpaQ_/view?usp=sharing https://drive.google.com/file/d/1PhETRPkgDag_DHWYlBFJL1qnodxDpaQ_/view?usp=sharing

As u can see the email field works fine, html form validation occurs every time when button is pressed.如您所见,email 字段工作正常,每次按下按钮时都会发生 html 表单验证。 In the Password field this is not the case, theres required a minimum of 7 characters and when i press submit button with only 3 characters filled in im not getting any warnings.在密码字段中,情况并非如此,至少需要 7 个字符,当我按下提交按钮时,只填写了 3 个字符,我没有收到任何警告。

Does someone know how to fix this?有人知道如何解决这个问题吗?

If you want to use the html5 form validation, the minlength attribute won't work.如果您想使用 html5 表单验证,则minlength属性将不起作用。 If you want to do that validation you should use pattern attribute and pass it like pattern=".{7,}" .如果您想进行该验证,您应该使用pattern属性并像pattern=".{7,}"一样传递它。 This is explained in this answer .这在此答案中进行了解释。

If you want to use the blazor validation (which is much better and recommended), you need to use data annotations in your User model.如果您想使用 blazor 验证(更好和推荐),您需要在您的User model 中使用数据注释。

For your particular case, you can use the MinLength attribute.对于您的特定情况,您可以使用MinLength属性。

public class User 
{
    // other propeties
 
    // Passing data annotations
    [MinLength(7, ErrorMessage = "Password Min Length is 7")]
    public string Password { get; set; }

}

And for the data annotations validation to work, don't forget that you need to have the DataAnnotationsValidator component inside the EditForm为了使数据注释验证正常工作,不要忘记您需要在EditForm中拥有DataAnnotationsValidator组件

<EditForm Model="@ModelValue">
    <DataAnnotationsValidator />

    @* rest of components *@
</EditForm>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM