Làm cách nào để sử dụng điều khiển đầu vào để chỉ cho phép các số nguyên không có số thập phân trong blazor mà không cần sử dụng js?

lập trình


Tôi đang tạo biểu mẫu một cách linh hoạt và tôi cần điều khiển đầu vào để chỉ chấp nhận số nguyên không cho phép nhập giá trị thập phân bao gồm cả dấu chấm. nhưng không có phương pháp nào cung cấp cho tôi kết quả chính xác.

Những gì tôi đã thử:

     <input type="text" class="form-control" id="@acs.CTL_NAME" @ref="@(_inputRefs[acs.SRNO - 1])" inputmode="numeric"
            @bind="bindData[acs.SRNO-1]" tabindex="@acs.TAB_INDEX" @oninput="@((ChangeEventArgs e) => HandleNumber(e,acs.SRNO-1))">

@code{
public string[] bindData; //array is initialized with null in other method which is working fine 

private void HandleNumber(ChangeEventArgs e,int index)
{
    string inputValue = e.Value?.ToString();

    inputValue = inputValue?.Replace(".", "");

    bindData[index] = Regex.Replace(inputValue, @"[^\d]", "");
    StateHasChanged();
}
}

Giải pháp 1

Sử dụng type="number"sau đó bạn có thể ẩn các rocker:

CSS
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none; /* hide spinner buttons*/
}

input[type=number] {
    -moz-appearance: textfield; /* Firefox - hide spinner buttons*/
}

コメント

タイトルとURLをコピーしました