jquery - 如果使用带有 RegEx 的 Jquery 电子邮件无效,则禁用提交按钮
问题描述
您能否向我展示一些在带有 RegEx 的电子邮件无效时禁用提交按钮的示例?如果您的答案或解释很简单,以便新手可以像我一样理解,那就太好了:)
谢谢
解决方案
只需在输入字段上添加事件即可触发验证功能。
如果emailRegex
不匹配,将disabled
属性设置为提交按钮。否则删除该属性
// Start script when everything is loaded
$(document).ready(function() {
// Email regex. It's very generic: check if "@" is present and has ".abc" end.
// There is no 100% correct email regex.
var emailRegex = /\w+@\w+\.\w{3}/;
// Add event to be triggered on input field change
$('#email').on('change', function() {
// Disable or enable submit button depending on regex validation
$('#submit').prop('disabled', emailRegex.test($(this).val()) == false);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<input type="text" placeholder="Email" id="email"/>
<button id="submit">Submit</button>
推荐阅读
- visual-studio-2019 - 安装 Visual Studio 2019 后我找不到 gacutil.exe
- python - 如何在整个类中声明一个全局变量?
- function - 如果返回特定值,则尝试使脚本仅向团队发送消息
- powerbi - 如何在 Power BI 的列中优化我的数据
- javascript - AsyncStorage 未定义检查
- javascript - 使用 ajax 将 $_POST 变量发送到 php
- json - 无法获取哪个查询加载内容
- javascript - 如何检测一个字符,如“”,是否是 JavaScript 不支持的字符?
- sql - 如何使用正则表达式在 Oracle 中提取括号外的单词?
- java - Mono.fromCallable 默认在调用线程上执行?