从代码中添加 javascript 会破坏页面上的验证!怎么处理呢?

发布于 2024-10-02 10:37:29 字数 886 浏览 6 评论 0原文

我所拥有的是按钮,仅当文本框长度为 8 时才应打开另一个页面。必须从代码中添加 JavaScript,因为它不简单地调用 bla.aspx,它更像是 bla.aspx?id=4&code=234 等...

我在服务器端

button.Attributes.Add("onclick","javascript:window.open(bla.aspx)");

和客户端都有这段代码,

<asp:TextBox ID="policyNumberTxt" runat="server" MaxLength="8" CausesValidation="true"></asp:TextBox>

<asp:RegularExpressionValidator ID="policyNumberTxtRev" runat="server"
    ControlToValidate="policyNumberTxt" ErrorMessage="Length must be 8."  
    ValidationExpression="{.{8}.}" ValidationGroup="bla" Display="Dynamic"></asp:RegularExpressionValidator>

<asp:Button ID="printBtn" CssClass="button" Text="Print" runat="server" CausesValidation="true" ValidationGroup="bla" />

发生的情况是,当我单击按钮页面 bla.aspx 打开时,甚至会触发回发。验证程序正在显示消息,但页面已打开并触发回发。

这要怎么处理呢?验证已损坏...

谢谢..

What I have is button that should open another page only if textbox length is 8. Javascript must be added from code because it does not simply calls the bla.aspx, it's more like bla.aspx?id=4&code=234 etc etc...

I have this code on server side

button.Attributes.Add("onclick","javascript:window.open(bla.aspx)");

on client side i have

<asp:TextBox ID="policyNumberTxt" runat="server" MaxLength="8" CausesValidation="true"></asp:TextBox>

<asp:RegularExpressionValidator ID="policyNumberTxtRev" runat="server"
    ControlToValidate="policyNumberTxt" ErrorMessage="Length must be 8."  
    ValidationExpression="{.{8}.}" ValidationGroup="bla" Display="Dynamic"></asp:RegularExpressionValidator>

<asp:Button ID="printBtn" CssClass="button" Text="Print" runat="server" CausesValidation="true" ValidationGroup="bla" />

What happens is that when I click the button page bla.aspx opens, and even postback is trigered. Validator is showing the message, but page is opened and postback trigered.

How to handle this? Validation is broken...

Thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘过的浮云 2024-10-09 10:37:29

为了防止提交表单,请添加“return false;”

button.Attributes.Add("onclick", "window.open('bla.aspx'); return false;")

但这也会阻止表单进行回发!

请注意,传递的 url 应该用引号引起来。
而且你不需要“javascript:”前缀,因为“Pointy”说

我认为验证正则表达式应该更严格,只允许这样的字母和数字

ValidationExpression="[A-Za-z0-9]{8}"

To prevent submitting the form add "return false;"

button.Attributes.Add("onclick", "window.open('bla.aspx'); return false;")

but that would also prevent the form from doing postback at all!

note that the passed url should be enclosed in quotes..
And you don't need the "javascript:" prefix as 'Pointy' said

I think the validation regular expression should be more strict to allow only letters and numbers like this

ValidationExpression="[A-Za-z0-9]{8}"
橙味迷妹 2024-10-09 10:37:29

尝试将客户端点击逻辑放在页面表单的 onsubmit 中?

Try putting the client click logic in the onsubmit of the page's form instead?

深海少女心 2024-10-09 10:37:29

找到答案此处

this.cmdSubmit.Attributes.Add("onclick","if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();if(Page_IsValid){window.open('upload_status.aspx','_blank','width=250,height=250');}");

Found answer here

this.cmdSubmit.Attributes.Add("onclick","if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();if(Page_IsValid){window.open('upload_status.aspx','_blank','width=250,height=250');}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文