prevent submit validation on Enter keydown

Hi team,


I attempted to try this solution but it seems SFtextbox doesn't accept handler event and preventdefault..

https://tinyurl.com/3yce3xnv

<input value="@name" type="text" @onkeydown="@onKeydown" @onkeydown:preventDefault="@isPreventKey" /> @code { private string name { get; set; } private bool isPreventKey { get; set; } private void onKeydown(KeyboardEventArgs args) { if(args.Key == "a") { this.isPreventKey = true; } } }



I also tried this solution, however it prevent the submit method upon "enter" but it still shows the validation. I want to show the validation error only upon clicking of "save" button,.



CssClass="e-outline e-success"

FloatLabelType="FloatLabelType.Always"

Placeholder="Scan Item Code"

Width="295px"

@onkeydown="KeyDownHandlerItemCodeAsync"

@bind-Value="@(ADdetailEdit.ItemCode)"

Focus="@OnFocusItemCode">





<button type="submit" @onclick="ShouldISubmit">Savebutton>


@Code{
public bool shouldsubmit { get; set; } public bool entersubmit { get; set; }

public void InvalidSubmit()

{

if (shouldsubmit)

{

toastService.ShowError("Adjustment was not applied. Check for valid entries."); //toast

}


}


private void HandleValidSubmit()
{
    if (shouldsubmit) {
        Console.WriteLine("OnValidSubmit");
    }
}

public void PreventSubmit(KeyboardEventArgs ev)
{
    if (ev.Key == "Enter")
        entersubmit = true;
}

public void ShouldISubmit()
{
    if (entersubmit)
        shouldsubmit = false;
    else
        shouldsubmit = true;
    entersubmit = false;
}


}


here is the model

public partial class InventoryAdjustmentDetailEdit

{

[required]

public string itemcode { get; set; }

}



Best Regards,

Tyrone



1 Reply

SP Sureshkumar P Syncfusion Team February 22, 2023 11:59 AM UTC

Hi Tyrone,

You can refer the below forum solution to prevent the edit form submit and validation using JS Interop

Find the solution here: https://www.syncfusion.com/forums/173262/disable-form-submit-when-pressing-enter-after-combobox-edit?reply=S2QA6w

Regards,

Sureshkumar P


Loader.
Up arrow icon