[英]Using Required field validator on an input with a watermark
My Required validation for a field doesn't work because even the user didn't input anything, the watermark text is sent to the server. 我对字段的必需验证不起作用,因为即使用户没有输入任何内容,水印文本也会发送到服务器。 What's the best way to handle this?
处理这个问题的最佳方法是什么? I'm using jquery and asp.net mvc.
我正在使用jquery和asp.net mvc。
You can use jQuery updnWatermark
plugin to insert watermarks on the page that don't write text to your textboxes, but insert new transparent element with your watermarks: updnWatermark
. 您可以使用
jQuery updnWatermark
插件在页面上插入水印,这些水印不会将文本写入文本框,但会在您的水印中插入新的透明元素: updnWatermark
。 Thats how you can validate your controls with Required validators without any tricks. 这就是你如何使用必需的验证器验证你的控件,没有任何技巧。
<script src="~/Scripts/jquery.updnWatermark.js" type="text/javascript"></script>
. <script src="~/Scripts/jquery.updnWatermark.js" type="text/javascript"></script>
。 Watermark
class. Watermark
类。 ToolTip
attribute that will contain watermark text to elements that needed watermarks. ToolTip
属性设置为需要水印的元素。 Add next code to the page: 将下一个代码添加到页面:
$.updnWatermark.attachAll({ cssClass: 'Watermark' });
The HTML5 placeholder attribute allows the browser to display a watermark and so the watermark value is not submitted back to the server on form posts. HTML5 占位符属性允许浏览器显示水印,因此水印值不会在表单帖子上提交回服务器。
Then add an extra bit of jQuery in your page for browsers that do not support placeholder
. 然后在页面中为不支持
placeholder
浏览器添加额外的jQuery。
If you don't want to include additional plugins you can always override the validator: 如果您不想包含其他插件,则可以始终覆盖验证器:
$.validator.methods.required = function (value, element) {
// some code
}
Remember though that this overrides the default required validator for all required checks on the page. 请记住,这会覆盖页面上所有必需检查的默认必需验证程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.