提问人:Chris Glenn 提问时间:9/1/2023 最后编辑:Chris Glenn 更新时间:9/7/2023 访问量:50
C# ASP.NET - 用户击键验证并将字符输入到正确的位置
C# ASP.NET - User keystroke validation and input character into correct place
问:
我希望有一个以 00:00 开头的时间字段,这是我想要的确切格式。
当用户键入时间时,我想读取击键并将其放在我预先分配的值内。
--编辑-- 删除了以前的验证代码,这些代码与了解如何在击键时进行验证无关。
答:
也许您使用内置的 textmode = time。
它不是 100% 像你问的那样,但你的规范有一些“问题”。
例如,如果我想要 10:30 或 1:30?
所以,1 和 3 表示 10:03 没有意义,因为我可能想要:
1:03
10:03
1:30
10:30
所以,在10:03打1,然后打3不能,从那以后你怎么进入10:30?
正如我所说,您可能最好使用默认文本模式 = 时间。
它将完成您要求的大部分工作,不允许用户输入非数字字符,以及您想要的大部分内容的格式。
所以,说我想要 10:30
如果我输入 1,0,3,那么这就是我得到的 例如,这个:
标记:
<h3>enter time</h3>
<asp:TextBox ID="txtTime" runat="server"
TextMode="Time">
</asp:TextBox>
所以,对于 10:30,然后是 1,0,3
现在,上面当然有 AM/PM
我们只需要尝试谷歌,看看是否可以删除 am/pm。
如果您需要超过 12 小时,那么上述内容很可能对您没有帮助。
评论
someway to have the server validate per keystroke.
为什么?这除了增加流量和服务器的工作负载,并延迟 UI 之外,什么也没提供。如果使用 Blazor Server,则所有内容都已在服务器上运行。发布您的代码
因此,这是我发现在用户键入时进行验证的内容。
它仍然显示他们在删除它之前输入的内容,但它尽可能接近我。
此外,如果有人知道一种方法可以从 eventclick 传入参数而无需在函数中声明它们,那么我可以在多个字段上重用它,传入不同的变量会很棒!
基本上,我为每个用户群设置了一个会话,当在内存中按下一个键时,该会话将保留该字段的值,当释放时,它将通过验证检查传递新值,如果它通过,那么一切都很好,否则它会重置内存存储中的值。
在程序下,我添加了 .cs
--法典
//Use for session control
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
});
var app = builder.Build();
// end the Session Control
// Add the Session Control then go to SessionController
app.UseSession();
在会话控制器下
--法典
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Products.Models;
namespace Products.Controllers
{
public class SessionController : Controller
{
private readonly IHttpContextAccessor _contextAccessor;
public SessionController(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
public FieldModel EventHandler(FieldModel model)
{
string sessionName = model.sessionName;
if (model.keystroke == "down")
{
//This is passing in the current value of the field. Just need to save it in the Session
string fieldString = JsonConvert.SerializeObject(model);
_contextAccessor.HttpContext.Session.SetString(sessionName, fieldString);
}
else if (model.keystroke == "up")
{
//TODO - This is passing in the new character. Get the session
FieldModel priorModel = new FieldModel();
string fieldString = _contextAccessor.HttpContext.Session.GetString(sessionName);
priorModel = JsonConvert.DeserializeObject<FieldModel>(fieldString);
bool validate = false;
//TODO - This is passing in the new character. Validate the value for the field
if (model.fieldType == "time")
{
if (model.fieldNewValue.Length < 5)
validate = true;
}
//TODO - If validation passes allow the update
if (validate == true)
{
//fieldString = JsonConvert.SerializeObject(model);
//_contextAccessor.HttpContext.Session.SetString(sessionName, fieldString);
}
//TODO - If validation fails reset the field to the current value
else
{
//fieldString = JsonConvert.SerializeObject(priorModel);
//_contextAccessor.HttpContext.Session.SetString(sessionName, fieldString);
model = priorModel;
}
}
return model;
}
}
}
对于网页视图,我使用了以下代码 --法典
<script>
function FieldValidatorD() {
let sessionName_ = `time`;
let fieldID_ = `time`;
let fieldType_ = `time`
let keystroke_ = `down`
let input = document.querySelector(`#` + fieldID_).value;
let json = {
sessionName: sessionName_,
fieldID: fieldID_,
fieldType: fieldType_,
fieldCurrentValue: input,
fieldNewValue: input,
keystroke: keystroke_
};
//console.log(json);
$.ajax({
type: `json`,
data: json,
url: `/Session/EventHandler`,
async: false,
success: function (data) {
//console.log(data);
//console.log(data.fieldNewValue);
}
})
};
function FieldValidatorU() {
let sessionName_ = `time`;
let fieldID_ = `time`;
let fieldType_ = `time`
let keystroke_ = `up`
let input = document.querySelector(`#`+fieldID_).value;
let json = {
sessionName: sessionName_,
fieldID: fieldID_,
fieldType: fieldType_,
fieldCurrentValue: input,
fieldNewValue: input,
keystroke: keystroke_
};
//console.log(json);
$.ajax({
type: `json`,
data: json,
url: `/Session/EventHandler`,
async: false,
success: function (data) {
//console.log(data);
//console.log(data.fieldNewValue);
//fill in the textbox by injecting the html into the field
$(`#` + fieldID_).val(data.fieldNewValue);
}
})
};
</script>
评论
blazor
评论
input type='time'
blazor