我正在使用jquery验证插件来验证注册表单.
每个文本输入字段都有在输入框中预先填充为值的指令
例如:对于文本输入框id ='Name',默认值将设置为'Enter Your Name'.我粘贴了下面的示例代码:
<input type="text" id="Name" value="Enter Your Name" onfocus="if(this.value == 'Your Name'){this.value = '';}" type="text" class="required" onblur="if(this.value == ''){this.value='Your Name';}" size="25" />
Run Code Online (Sandbox Code Playgroud)
我需要知道的是如何在验证规则中指定,如果输入框包含默认消息或空值,则插件会抛出必填字段错误.
非常感谢.
小智 19
我认为这更好:
jQuery.validator.addMethod("defaultInvalid", function(value, element)
{
return !(element.value == element.defaultValue);
});
Run Code Online (Sandbox Code Playgroud)
我通过使用validator.addMethod创建自定义验证来解决问题,这非常简单.
我在下面粘贴了我的验证块的片段:
//1. Define custom validation
$(document).ready(function()
{
jQuery.validator.addMethod("defaultInvalid", function(value, element)
{
switch (element.value)
{
case "Your Name": -- this will be the default value in the Name text box
if (element.name == "Your Name")
return false;
}
}
//2. Include custom validation in the rules for your element
$("#aspnetForm").validate(
{
rules:
{
Name: "required defaultInvalid"
},
messages:
{
Name: "Please input your name"
}
})
}
Run Code Online (Sandbox Code Playgroud)
我已经对表单中具有默认值的所有元素使用了相同的内容.这非常有效.
| 归档时间: |
|
| 查看次数: |
35543 次 |
| 最近记录: |