我正在使用正则表达式禁用使用Apple电子邮件注册
if (str.match('(@me.com)') || str.match('(@icloud.com)') || str.match('(@mac.com)'))
如果我使用浏览器控制台运行它,代码工作正常但我最初无法使用它,我将它包装在$(document).ready(function () { .. }
中,而且模糊功能似乎永远不会触发。以下是代码以及CodePen
<div class="content">
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" role="form">
<div class="warning email-warning" style="display:none;">A non-Apple email address is required due to issues with our mail server. Sorry about that!</div>
<input type="text" name="email" placeholder="" value="" id="Email"/>
<input type="password" name="password" placeholder="Password" value="" />
<input style="font-size: 1.5rem;" type="submit" value="Login" class="btn btn-default" />
<div class="already">Don't have an account? <a href="/index.php?route=account/register">Register</a></div>
<a class="block text-center" href="/index.php?route=account/forgotten">Forgot your password?</a>
</form>
</div>
function emailValidate(){
var str = $('#Email').val();
$('#Email').blur(function() {
if (str.match('(@me.com)') || str.match('(@icloud.com)') || str.match('(@mac.com)')) {
$('.email-warning').css('display', 'block')
}
});
}
emailValidate()
答案 0 :(得分:3)
您需要使用val()
将值重新分配给模糊事件回调中的变量str
。
function emailValidate(){
$('#Email').blur(function() {
var str = $(this).val();
if (str.match('(@me.com)') || str.match('(@icloud.com)') || str.match('(@mac.com)')) {
$('.email-warning').css('display', 'block')
}
alert(str)
});
}
emailValidate()
答案 1 :(得分:0)
当模糊事件触发时,您必须获取目标对象#Email的值。在代码中,绑定模糊事件时会获得该值。 试试
function emailValidate(){
$('#Email').blur(function() {
var str = $('#Email').val();
if (str.match('(@me.com)') || str.match('(@icloud.com)') || str.match('(@mac.com)')) {
$('.email-warning').css('display', 'block')
}
});
}
emailValidate();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content">
<form role="form">
<div class="warning email-warning" style="display:none;">A non-Apple email address is required due to issues with our mail server. Sorry about that!</div>
<input type="text" name="email" placeholder="" value="" id="Email"/>
<input type="password" name="password" placeholder="Password" value="" />
<input style="font-size: 1.5rem;" type="submit" value="Login" class="btn btn-default" />
<div class="already">Don't have an account? <a href="/index.php?route=account/register">Register</a></div>
<a class="block text-center" href="/index.php?route=account/forgotten">Forgot your password?</a>
</form>
</div>
&#13;
希望这会有所帮助。 问候。