如何在电子邮件验证期间仅为Regex模式更改验证消息

时间:2016-11-24 14:49:52

标签: javascript html regex html5 validation

Iam使用html5验证电子邮件..但是它没有验证.com或.co.So Iam使用正则表达式

var d = {
  "id": 12578834,
  "title": "Joint R&D Has Its Ups and Downs",
  "url": "http://semiengineering.com/joint-rd-has-its-ups-and-downs/",
  "num_points": 1,
  "num_comments": 0,
  "author": "Lind5",
  "created_at": "9/26/2016 2:28"
}

var today = new Date();
var createdOn = new Date(d.created_at);
var msInDay = 24 * 60 * 60 * 1000;

createdOn.setHours(0,0,0,0);
today.setHours(0,0,0,0)

var diff = (+today - +createdOn)/msInDay
console.log(diff)

如果我不给@它正在给html5验证消息,这很好..但是如果我们不给。并且在com之后它将正则表达式消息作为“请匹配所请求的格式”。我们如何将此消息更改为有意义的消息。但其他消息不应更改。

[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$

由于

的jsfiddle:https://jsfiddle.net/T29Kx/369/

1 个答案:

答案 0 :(得分:1)

每个浏览器都不支持(我认为Safari不支持它)但你可以使用oninvalid和setCustomValidity方法



<form>
  <input type="email" class="form-control" id="inputEmail3" placeholder="Email" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" oninvalid="setCustomValidity('Plz enter valid email')" required/>
  <input type="submit">
</form>
&#13;
&#13;
&#13;