重写 dijit 验证器函数并使用 regExp 属性

发布于 2024-08-13 07:28:00 字数 886 浏览 4 评论 0原文

我是 dojo 的新手,确实可以在以下 2 个字段验证示例中使用一些帮助。

在下面的 dijit.form.ValidationTextBox 字段示例中,指定验证器属性似乎覆盖了 regExp 的使用。 (即该字段不再遵守 regExp 规则)。我怎样才能让它两者兼而有之?

<输入 dojoType="dijit.form.ValidationTextBox"
类型=“密码”
名称=“密码2”
id="密码2"
最大长度=“50”
修剪=“真”
regExp="[\w]+"
必需=“真”
validator="return this.value == dijit.byId('password').value"
invalidMessage="确认密码必须与密码一致" />

我还有另一个类似的示例,其中一个字段取决于另一个字段的值,但我的语法不正确。

<输入 dojoType="dijit.form.ValidationTextBox" 类型=“文本”
名称=“家庭电话”
id="家庭电话"
样式=“宽度:20%”
最大长度=“10”
修剪=“真”
必需=“假”
regExp="[\d]{10}"
validator="return (dijit.byId('preferredContactMethod').value == "home") && (this.value != null)"
invalidMessage="需要家庭电话(即 9198887777)"
/>

I'm new to dojo and could really use some help with the following 2 field validation examples.

In the following example of a dijit.form.ValidationTextBox field specifying the validator property seems to override the use of the regExp. (ie the field no longer adheres to the regExp rule). How to I make it do both?

<input dojoType="dijit.form.ValidationTextBox"
type="password"
name="password2"
id="password2"
maxLength="50"
trim="true"
regExp="[\w]+"
required="true"
validator="return this.value == dijit.byId('password').value"
invalidMessage="Confirmation password must match password"
/>

I have another similar example where one field depends on the value of another, but I don't have the syntax correct.

<input dojoType="dijit.form.ValidationTextBox"
type="text"
name="homePhone"
id="homePhone"
style="width:20%"
maxLength="10"
trim="true"
required="false"
regExp="[\d]{10}"
validator="return (dijit.byId('preferredContactMethod').value == "home") && (this.value != null)"
invalidMessage="Home phone required (ie. 9198887777)"
/>

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

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

发布评论

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

评论(1

桃气十足 2024-08-20 07:28:00

正确的; dijit.form.ValidationTextBox.prototype.validator() 的默认实现是将 this.value 与 this.regExp 进行匹配,并检查各种其他约束,例如 this.required。查看源代码以了解它是如何完成的。如果您覆盖它,您就需要自己提供实现。您的实现可能会选择委托给原型方法,并用您自己的测试对结果进行逻辑“与”。我想你也可以重写 isValid 。

Correct; the default implementation of dijit.form.ValidationTextBox.prototype.validator() is to match this.value against this.regExp, as well as check against the various other constraints like this.required. Take a look at the source to see how it's done. If you override that, you're on your own to provide an implementation. Your implementation might choose to delegate to the prototype method and logically 'and' the results with your own tests. You could also override isValid, I suppose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文