使用 jquery.validationEngine.js 处理默认表单值

发布于 2024-11-08 15:48:42 字数 739 浏览 0 评论 0原文

我使用 jquery.validationEngine.js 进行表单验证。

我从 下载了这个js http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

这个网站。但它不适用于检查默认值的验证,例如正如我有名字字段,其值为“名字”。我想要验证以检查该字段不应为空,但它不起作用,因为它包含默认值“名字”。

另外我希望这应该在 jquery.validationEngine.js 文件中工作,因为我必须对表单和表单进行多次验证。我正在使用这个js。

我的领域是

<input type="text" id="Uname" name="Uname" value="User Name" onfocus="if(this.value=='User Name')this.value='';" onblur="if(this.value=='')this.value='User Name';" />

如果有人使用此文件请告诉我&帮助解决这个问题。

I am using jquery.validationEngine.js for form validation .

I was downloaded this js from http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

this site.But it not works for checking validation for default value such as I have first name field whose value is "First Name".I want validation for checking that this field should not be blank but it not works because it contains default value "First Name".

Also I want this should work in jquery.validationEngine.js file because I have to many validations on form & I am using this js.

My field is

<input type="text" id="Uname" name="Uname" value="User Name" onfocus="if(this.value=='User Name')this.value='';" onblur="if(this.value=='')this.value='User Name';" />

If anyone using this file let me know & help to solve this problem.

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

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

发布评论

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

评论(3

悟红尘 2024-11-15 15:48:42

如果您希望使用validationEngine按照您描述的方式验证表单,那么根据文档似乎至少有三种解决方案。

1) 您可以在翻译文件中为每个默认文本值创建一个新的自定义正则表达式,然后将该自定义正则表达式添加到相关表单项。这可能是您的选择中最棘手的,因为您将需要使用否定前瞻或类似的东西来使正则表达式正确。

2) 让验证器调用您编写的一个或多个函数来处理特殊情况。我不知道validationEngine是否允许您将参数传递给函数——文档对此没有任何说明——所以我猜它不会。这可能意味着您需要为每个默认值编写一个单独的函数,或者使用全局变量来指示您正在检查的默认值。代码片段中的 Uname 字段的函数可能如下所示:

function checkUname(field, rules, i, options){
  if (field.val() == "User Name") {
     return "You must type in your username.";
  }

定义该函数后,您可以使用类似以下内容来使用它:

<input type="text" class="form validate[required,funcCall[checkUname]]" id="Uname" name="Uname" value="User Name" onfocus="if(this.value=='User Name')this.value='';" onblur="if(this.value=='')this.value='User Name';" />

3) 您可以编写一个 JavaScript 函数来执行表单中的每个字段,如果找到默认值,则将其更改为空字符串。然后将该函数附加到表单中的 onsubmit 事件。这可能是最简单的选项,但这取决于您的函数在validationEngine代码之前运行。如果不是这样的话,那就行不通。

If you wish to use validationEngine to validate your form the way you describe, there appear to be at least three solutions based on the documentation.

1) You can create a new custom regex in the translation file for each default text value, and then add that custom regex to the relevant form item. This is probably the trickiest of your options, as you will need to use a negative lookahead or something similar to get the regex correct.

2) Have the validator call one or more functions that you write to handle your special cases. I don't know if validationEngine allows you to pass parameters to the function--the documentation says nothing about that--so I'd guess it doesn't. This may mean that you will need to either write a separate function for each default value or else use a global variable to indicate the default value you are checking for. A function for your Uname field in your code snippet might look like this:

function checkUname(field, rules, i, options){
  if (field.val() == "User Name") {
     return "You must type in your username.";
  }

Once that function is defined, you can use something like this to use it:

<input type="text" class="form validate[required,funcCall[checkUname]]" id="Uname" name="Uname" value="User Name" onfocus="if(this.value=='User Name')this.value='';" onblur="if(this.value=='')this.value='User Name';" />

3) You can write a single JavaScript function that goes through each field in your form and, if it finds the default value, changes it to an empty string. Then attach that function to the onsubmit event in your form. This may be the easiest option, but it depends on your function running before the validationEngine code. If that's not the case, then it won't work.

偏闹i 2024-11-15 15:48:42

这是一个很好的例子
如何验证具有默认值的可选字段?

否则,请参阅我发布的相同问题,并可能进行更改,

jQuery.validator.addMethod("defaultInvalid", function(value, element) {
  if (element.value == element.defaultValue) return false;
}

而不是开关/案例

<input type="text" id="Uname" name="Uname" value="User Name" onfocus="if(this.value==this.defaultValue) this.value=''" 
onblur="if(this.value=='') this.value=this.defaultValue" />

Here is a good example
How do you validate optional fields with default value?

Otherwise see the question I posted as identical question with the possible change

jQuery.validator.addMethod("defaultInvalid", function(value, element) {
  if (element.value == element.defaultValue) return false;
}

instead of the switch/case

<input type="text" id="Uname" name="Uname" value="User Name" onfocus="if(this.value==this.defaultValue) this.value=''" 
onblur="if(this.value=='') this.value=this.defaultValue" />
む无字情书 2024-11-15 15:48:42

您应该使用 HTML5 placeholder 属性而不是 JavaScript 设置占位符值。

You should set the placeholder value using the HTML5 placeholder attribute instead of JavaScript.

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