检查使用 MaskedEditExtender 扩展的文本框的值是否有效?
下面是我的代码:
<asp:TextBox
ID="FromDateTextBox"
runat="server" />
<asp:ImageButton
ID="FromDateImageButton"
runat="server"
ImageUrl="~/images/calander.png" />
<ajaxkit:CalendarExtender
ID="FromDate"
runat="server"
TargetControlID="FromDateTextBox"
CssClass="CalanderControl"
PopupButtonID="FromDateImageButton"
Enabled="True" />
<ajaxkit:MaskedEditExtender
id="FromDateMaskedEditExtender"
runat="server"
targetcontrolid="FromDateTextBox"
Mask="99/99/9999"
messagevalidatortip="true"
onfocuscssclass="MaskedEditFocus"
oninvalidcssclass="MaskedEditError"
masktype="Date"
displaymoney="Left"
acceptnegative="Left"
errortooltipenabled="True" />
<ajaxkit:MaskedEditValidator
id="FromDateMaskedEditValidator"
runat="server"
controlextender="FromDateMaskedEditExtender"
controltovalidate="FromDateTextBox"
emptyvaluemessage="Date is required"
invalidvaluemessage="Date is invalid"
display="Dynamic"
tooltipmessage="Input a date"
emptyvalueblurredtext="*"
invalidvalueblurredmessage="*"
validationgroup="MKE" />
我在 @Page 指令中设置了 Culture="auto" UICulture="auto"
,在脚本中设置了 EnableScriptGlobalization="true" EnableScriptLocalization="true"
经理在我的文本框中具有客户文化特定的日期格式。
我的页面上还有一个 Go
按钮,我将在该按钮上进行部分回发。因此,我想在单击 Go
按钮时验证 javascript 中的 FromDateTextBox
。
更新
我知道如何创建 JavaScript 点击处理程序。但是因为屏蔽编辑器已经在验证焦点转移时的日期,所以我认为应该有一些布尔属性(如 IsValid)由它公开,这将允许我查看文本框是否包含有效日期。
进一步试验
我还尝试了下面的代码,Page_Validators[f].isvalid
始终返回 true,即使日期无效并且 MaskEditValidator 在文本框附近显示一个红星。
function isDateValid() {
var b = true;
for (var f = 0; f < Page_Validators.length; f++) {
if (!Page_Validators[f].isvalid)
b = false;
}
return b;
}
$('#GoButton').click(function() {
if (!isDateValid()) {
return false;
}
loadProducts();
});
Below is my code:
<asp:TextBox
ID="FromDateTextBox"
runat="server" />
<asp:ImageButton
ID="FromDateImageButton"
runat="server"
ImageUrl="~/images/calander.png" />
<ajaxkit:CalendarExtender
ID="FromDate"
runat="server"
TargetControlID="FromDateTextBox"
CssClass="CalanderControl"
PopupButtonID="FromDateImageButton"
Enabled="True" />
<ajaxkit:MaskedEditExtender
id="FromDateMaskedEditExtender"
runat="server"
targetcontrolid="FromDateTextBox"
Mask="99/99/9999"
messagevalidatortip="true"
onfocuscssclass="MaskedEditFocus"
oninvalidcssclass="MaskedEditError"
masktype="Date"
displaymoney="Left"
acceptnegative="Left"
errortooltipenabled="True" />
<ajaxkit:MaskedEditValidator
id="FromDateMaskedEditValidator"
runat="server"
controlextender="FromDateMaskedEditExtender"
controltovalidate="FromDateTextBox"
emptyvaluemessage="Date is required"
invalidvaluemessage="Date is invalid"
display="Dynamic"
tooltipmessage="Input a date"
emptyvalueblurredtext="*"
invalidvalueblurredmessage="*"
validationgroup="MKE" />
I've set Culture="auto" UICulture="auto"
in @Page directive and EnableScriptGlobalization="true" EnableScriptLocalization="true"
in script manager to have client culture specific date format in my textbox.
I also have a Go
button on my page on which I will do a partial post back. So, I want to validate the FromDateTextBox
in javascript when the Go
button is clicked.
UPDATE
I know how to create a javascript click handler. But because masked editor is already validating the date on focus shift, I'm thinking there should be some boolean property (like IsValid) exposed by it which will allow me to see if the text box contains valid date.
FURTHER TRIALS
I also tried below code and Page_Validators[f].isvalid
always returns true even when the date is invalid and MaskEditValidator shows me a red star near the Text box.
function isDateValid() {
var b = true;
for (var f = 0; f < Page_Validators.length; f++) {
if (!Page_Validators[f].isvalid)
b = false;
}
return b;
}
$('#GoButton').click(function() {
if (!isDateValid()) {
return false;
}
loadProducts();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,您的意思是您的 MaskedEditValidator 在单击 GO 按钮时不起作用?或者你想自己写一些js来检查日期?在这种情况下,您可以将客户端 onclick 事件添加到 GO 按钮。代码后面的一个例子是:
在页面文件中添加一个 javascript 函数:
现在如果日期无效,CheckDate() 将返回 false,goButton 事件也将返回而不回发。
Sorry do you mean that your MaskedEditValidator is not working when clicking GO button? Or if you want to write some js to check the date yourself? In this case you can add a client onclick event to GO button. One example in code behind is:
And in the page file add a javascript function:
Now if the date is not valid,the CheckDate() will return false, the goButton event will also return without posting back.
我解决了。基本上,当在文本框中输入无效日期时,MaskedEditExtender 会更改文本框提供的 (
OnInvalidCssClass="MaskedEditError"
) 类,我将其作为验证检查点。I resolved it. Basically when there is an invalid date entered in the textbox the MaskedEditExtender changes the provided (
OnInvalidCssClass="MaskedEditError"
) class of the textbox and I picked it up as a checking point for validation.