Validating an e-mail based on a specific domains

This is a registration for for my site.
I want to enable registration only for users with an email from a specific domain (i.e walla.co.il).
in theory this code should work but in practice it does not.
what am I doing wrong.

$('button').on('click', function() {
  str = $('#email').val();
  str = str.split('@').slice(1);

  var allowedDomains = ['walla.co.il', 'gmail.com', 'yahoo.com'];

  if ($.inArray(str[0], allowedDomains) !== -1) {
    alert(str + ' is allowed');
  } else {

  }
});
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
<form id="signup" class="js_ajax-forms" data-url="core/signup.php">
  <input name="first_name" type="text" class="form-control" placeholder='First name' required>
  <input name="last_name" type="text" class="form-control" placeholder='Last name' required>
  <input name="username" type="text" class="form-control" placeholder='Username' required>
  <input name="email" type="email" class="form-control" id="email" placeholder='Email' required>
  <input name="password" type="password" class="form-control" placeholder='Password' required>
  <select name="gender" id="gender" class="form-control" required>
    <option value="none">Select Sex:</option>
    <option value="male">Male</option>
    <option value="female">Female</option>
    <option value="other">Other</option>
  </select>
  <input type="checkbox" class="custom-control-input" name="newsletter_agree" id="newsletter_agree">
  <label class="custom-control-label" for="newsletter_agree">
                                        I expressly agree to receive the newsletter
                                    </label>
  <input type="checkbox" class="custom-control-input" name="privacy_agree" id="privacy_agree">
  <label class="custom-control-label" for="privacy_agree"> By creating your account, you agree to our 
    <a href="http://127.0.0.1//static/terms" target="_blank">Terms</a> & 
    <a href="http://127.0.0.1//static/privacy" target="_blank">Privacy Policy</a> </label>
  <button type="submit" class="btn btn-block btn-success bg-gradient-green border-0 rounded-pill">Sign Up</button></form>

48 thoughts on “Validating an e-mail based on a specific domains”

Leave a Comment