iPad safari应该符合html5标准,但似乎所需的元素不起作用。任何人都知道为什么,或者有一个不需要大量JavaScript的体面解决方案?
我的代码
<input type=email class=input placeholder="Email" name="email" required>
iOS尚未支持:when can I use: required。
这是该问题的jQuery解决方案,它突出显示了以小指颜色失败的输入字段。
$('form').submit(function(){
var required = $('[required="true"]'); // change to [required] if not using true option as part of the attribute as it is not really needed.
var error = false;
for(var i = 0; i <= (required.length - 1);i++)
{
if(required[i].value == '') // tests that each required value does not equal blank, you could put in more stringent checks here if you wish.
{
required[i].style.backgroundColor = 'rgb(255,155,155)';
error = true; // if any inputs fail validation then the error variable will be set to true;
}
}
if(error) // if error is true;
{
return false; // stop the form from being submitted.
}
});
从iOS 10.3开始,支持此属性。电子邮件类型也需要写@符号等等......
如果您已经在使用jQuery,Modernizr和yepnope,这是处理它的一种方法。如果你不是那么这将添加很多额外的JavaScript。
我想你可以在这样的提交动作之前做点什么
<form name="myForm" action="valid.html" onsubmit="checkValid()" method="post">
... ...
</form>
按下submit
按钮后,checkValid()
在它实际提交之前被唤起。 true
的返回值将继续提交操作。
请参考this post进一步解释。:)
如果您使用PHP,则可以添加这样的验证
function validation(){
if(isset($_POST['submit'])){
$email = $_POST['email'];
if(empty($email)){
echo $error = "Your email cannot be empty";
} else {
return true; //or do something next here
}
}
然后在表单之前在php中添加此函数。