Hi santosgernan1,
Refer below example.
Model
public class PersonModel
{
public string Salary { get; set; }
}
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(PersonModel person)
{
if (ModelState.IsValid)
{
// Validation success.
}
return View();
}
}
View
@model US_MobileNumber_Validation_MVC_Core.Models.PersonModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
</style>
</head>
<body>
<form method="post" asp-controller="Home" asp-action="Index">
<label asp-for="Salary"></label>
<input id="txtSalary" type="text" asp-for="Salary" />
<hr />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#txtSalary").on('keyup', function (event) {
if (event.which >= 37 && event.which <= 40) {
return;
}
$(this).val(function(index, value) {
return value.replace(/[^\d.-]|(?!^)-/g, "")
.replace(/^([^.]*\.)(.*$)/, (_, g1, g2) => g1 + g2.replace(/\./g, ''))
.replace(/\.(\d{2})\d+/, '.$1')
.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
});
});
});
</script>
</body>
</html>
Screenshot
