关于 html:HTML5 Validation with Opera and Safari
HTML5 Validation with Opera and Safari
我在 Opera 和 Safari 上的 HTML5 验证有问题。
在 Opera 中,消息气泡不显示任何文本,而在 Safari 中,当我按下提交按钮时不会发生验证。在 IE、Mozilla 或 Chrome 中,验证工作得很好。谁能告诉为什么会这样?
我在表单中的输入具有 html5 标准验证,具有 required 属性,就是这样。
我试图在网上搜索这个主题,但没有找到。
请帮帮我。
谢谢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<form class="sign-in-form" action="" method="post"> <li> <label> <span>Username</span> <input placeholder="Please enter your username" name="username" type="text" tabindex="1" title="It must contain the username that you have chosen at registration" required autofocus> </label> </li> <li> <label> <span>Password</span> <input placeholder="Please enter your password" name="password" type="password" tabindex="2" title="It must contain the password that you have chosen at registration" required> </label> </li> <li> <button name="sign-in-btn" type="submit" id="sign-in-submit">Sign In</button> </li> </form> |
相关讨论
- 您能否向我们展示一些代码,以便我们尝试找出发生了什么?否则真的没什么可做的。
- 我已经编辑了帖子以向您展示代码。我真的不知道出了什么问题
这是 opera 中的一个奇怪错误:使用 @font-face 网络字体时不显示消息。我也遇到过这个问题。选择像 Arial 这样的普通字体,就会显示消息。 Safari 不支持 html5 验证(Safari 部分支持但没有验证气泡)。我的建议是:使用 webshims lib (http://afarkas.github.io/webshim/demos/) - 用于验证等许多功能的出色 polyfill。
相关讨论
- yeei ...这就是问题所在。十分感谢你分享这些信息。
- 想知道是否有人知道 Opera 的解决方法。我被迫使用自定义字体进行输入。
- Opera 15 没有这个问题了
- 我仍然在 Opera 20 中得到这个。它不提交表单,并巧妙地以蓝色突出显示违规字段,但没有验证工具提示。该工具提示出现在最新的 Chrome、IE 和 Firefox 中。
我遇到了同样的问题,就这样解决了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
(function() { var loadScript = function(src, loadCallback) { var s = document.createElement('script'); s.type = 'text/javascript'; s.src = src; s.onload = loadCallback; document.body.appendChild(s); }; // http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0; var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; if (isSafari || isOpera) { loadScript('//code.jquery.com/jquery-2.1.4.min.js', function () { loadScript('//cdnjs.cloudflare.com/ajax/libs/webshim/1.15.10/dev/polyfiller.js', function () { webshims.setOptions('forms', { overrideMessages: true, replaceValidationUI: false }); webshims.setOptions({ waitReady: true }); webshims.polyfill(); }); }); } })(); |
只需在页面末尾添加此片段即可。如果您已经导入了加载 jQuery 步骤,则可以删除它!
您可以添加以下内容(附图片):
http://i.stack.imgur.com/sMe9Z.png
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<style type="text/css"> input.invalid, select.invalid, textarea.invalid { border-color: #ff0000; background-image: url('../img/required.png'); background-position: right top; background-repeat: no-repeat; -moz-box-shadow: none; } input:required:valid, textarea:required:valid, select:required:valid { border: 1px solid #cccccc; background-image: none; } input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; } </style> <form action="" method="get" accept-charset="utf-8"> <input type="text" name="name" required> </form> <script type="text/javascript"> // Force Validation function html5Validation () { //Check if validation supported && not safari return (typeof document.createElement('input').checkValidity === 'function') && !(navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0); } $('form').submit(function(){ if(!html5Validation()) { var isValid = true; var $inputs = $(this).find('[required]'); $inputs.each(function(){ var $input = $(this); $input.removeClass('invalid'); if(!$.trim($input.val()).length) { isValid = false; $input.addClass('invalid'); } }); if(!isValid) { return false; } } }); |

相关讨论
- 你用的是图片?只需使用基本文本和 css 让它看起来像你想要的那样。图片加载速度较慢,无法翻译
THE END
二维码