Javascript 验证不适用于 .Net 内容页面
我想知道是否还有其他人遇到过以下问题。
在单个非链接(到母版页)的 .aspx 页面上,我正在执行简单的 JS 验证:
function validateMaxTrans(sender, args) {
// requires at least one digit, numeric only characters
var error = true;
var regexp = new RegExp("^[0-9]{1,40}(\.[0-9]{1,2})?$");
var txtAmount = document.getElementById('TxtMaxTransAmount');
if (txtAmount.value.match(regexp) && parseInt(txtAmount.value) >= 30) {
document.getElementById('maxTransValMsg').innerHTML = ""
args.IsValid = true;
}
else {
document.getElementById('maxTransValMsg').innerHTML = "*";
args.IsValid = false;
}
}
然后,一旦我将其移动到母版页的内容页面中,我就会得到 txtAmount 为空。
当尝试使用主页面/内容页面执行客户端 JS 验证时,是否有不同的方式来访问 DOM?
I'm wondering if anyone else has experienced the following issue.
On a single non-linked (to a master page) .aspx page, I'm performing simple JS validations:
function validateMaxTrans(sender, args) {
// requires at least one digit, numeric only characters
var error = true;
var regexp = new RegExp("^[0-9]{1,40}(\.[0-9]{1,2})?$");
var txtAmount = document.getElementById('TxtMaxTransAmount');
if (txtAmount.value.match(regexp) && parseInt(txtAmount.value) >= 30) {
document.getElementById('maxTransValMsg').innerHTML = ""
args.IsValid = true;
}
else {
document.getElementById('maxTransValMsg').innerHTML = "*";
args.IsValid = false;
}
}
Then as soon as I move this into a Master page's content page, I get txtAmount is null.
Is there a different way to access the DOM when attempting to perform client-side JS validation with master/content pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看母版页中渲染页面的源代码。许多元素都有一个 ID,如 ControlX$SubControlY$txtMaxTransAmount ...您需要相应地调整验证。我通常会将 ID 注入到客户端文档中。
我会将其放在内容区域末尾之前,以确保控件已呈现。这样你就可以简单地使用window.controls.txtAmount来引用服务器端控件的标签id。您甚至可以直接将右侧值设为 document.getElementById('...') 。
Look at the source for your rendered page within the master page. Many elements will have an ID like ControlX$SubControlY$txtMaxTransAmount ... you'll need to adjust your validation accordingly. I will often just inject the IDs into the client doc..
I'd put this right before the end of your content area, to make sure the controls are rendered already. This way you can simply use window.controls.txtAmount to reference the server-side control's tag id. You could even make the right-side value a document.getElementById('...') directly.
您使用的是 asp 文本框吗?如果是这样,我相信您需要执行类似
document.getElementById('<%= txtMaxTransAmount.ClientID %>')
的操作。希望这有帮助
汤姆
Are you using asp textboxes? If so I believe you need to do somethign like
document.getElementById('<%= txtMaxTransAmount.ClientID %>')
.Hope this helps
Tom