简体   繁体   English

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

[英]How to change validation message only for Regex pattern during email validation

Iam using html5 validation for email ..But there it is not validating for .com or .co.So Iam using regex for that Iam对电子邮件使用html5验证..但是没有对.com或.co进行验证。所以Iam为此使用正则表达式

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

If i dont give @ it is giving html5 validation message which is fine..But if we dont give . 如果我不给@,则给出html5验证消息,这很好。但是,如果我们不给。 and following com it is giving regex message as "Please match the requested format"..How can we change this message to a meaningful one.But the other messages shouldnt be changed. 并在com后面给出正则表达式消息为“请匹配请求的格式”。我们如何将该消息更改为有意义的消息。但是其他消息不应更改。

 <input type="email" class="form-control" id="inputEmail3" placeholder="Email" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{1,63}$" required/>

Thanks 谢谢

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

It's not supported on every browser (i think safari lacks support for it) but you can use the oninvalid and setCustomValidity methods 并非所有浏览器都支持该功能(我认为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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM