简体   繁体   English

模糊功能不是第一位

[英]blur function not being first

I am using regex to disable registering with a apple email 我正在使用正则表达式禁用向Apple电子邮件注册

if (str.match('(@me.com)') || str.match('(@icloud.com)') || str.match('(@mac.com)'))

The code is working fine if I run it with the browser console but I cant get it to work initially, Ive wrapped it in a $(document).ready(function () { .. } as well the blur function just never seems to fire. Here is the code below as well as CodePen 如果我使用浏览器控制台运行该代码,则该代码运行良好,但最初无法正常工作,我将其包装在$(document).ready(function () { .. } ,以及模糊函数似乎从未火,这是下面的代码以及CodePen

Html HTML

<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>

JS JS

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()

You need to re-assign the value using val() to variable str inside blur event callback. 您需要使用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()

you must get value of target object #Email when the blur event trigger. 当模糊事件触发时,您必须获取目标对象#Email的值。 In your code, you get that value when you bind blur event. 在您的代码中,绑定模糊事件会获得该值。 Try with 试试看

 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(); 
 <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> 

Hope this helps. 希望这可以帮助。 Regards. 问候。

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

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