Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building a form dynamically and i need a input control to accept only whole number which doesnt allow to enter decimal values including dot. but None of the methods are providing me accurate result.

What I have tried:

     <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();
}
}
Posted
Updated 13-Feb-24 20:37pm
v2

1 solution

Use type="number", then you can hide the rockers:
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*/
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900