Question : Block E-mail Address with Jquery

Hello experts!

Is there a way to block a user entering an e-mail address to an input field with jquery? I have found some code but it's just not working for me.

Here's the HTML:
<tr>
   <td><label for="Name_A">Agent Name: </label></td>
   <td><input id="Name_A" name="Name_A" type="text" /></td>
</tr>

And I have attached the jquery below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
//http://yensdesign.com/2009/01/how-validate-forms-both-sides-using-php-jquery/

$(document).ready(function() {
    //global vars
    var form = $("#customForm");
    var orderedBy = $("#orderedBy");
    var nameInfo = $("#nameInfo");
    var phoneNumber = $("#phoneNumber");
    var phoneInfo = $("#phoneInfo");    

    var emailaddressVal = $("#Name_A").val();
    var emailblockReg = /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/;

    //On blur
    orderedBy.blur(validateName);
    phoneNumber.blur(validatePhone);
    Name_A.blue(validateEmail);

    //On key press
    orderedBy.keyup(validateName);
    orderedBy.keyup(validatePhone);
    Name_A.keyup(validateEmail);

    //On Submitting
    form.submit(function() {
        if (validateName() & validatePhone()) {
            return true
        } else {
            alert('Please check that all required boxes are filled.');
            return false;
        }
    });

    //validation functions
    function validateName() {
        //if it's NOT valid
        if (orderedBy.val().length < 1) {
            orderedBy.addClass("error");
            nameInfo.text("!");
            nameInfo.addClass("error");
            return false;
        }
        //if it's valid
        else {
            orderedBy.removeClass("error");
            nameInfo.text("*");
            nameInfo.removeClass("error");
            return true;
        }
    }
    function validatePhone() {
        //if it's NOT valid
        if (phoneNumber.val().length < 1) {
            phoneNumber.addClass("error");
            phoneInfo.text("!");
            phoneInfo.addClass("error");
            return false;
        }
        //if it's valid
        else {
            phoneNumber.removeClass("error");
            phoneInfo.text("*");
            phoneInfo.removeClass("error");
            return true;
        }
    }
    function validateEmail() {
        if (!emailblockReg.test(emailaddressVal)) {
            alert('Not Today!');
            return false;
        }
    }
});

Answer : Block E-mail Address with Jquery

You can also try the code below, which would accept any input except an email in the name field
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
<!DOCTYPE html>
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script type="text/javascript">
 $(document).ready(function() {

	  $('#Name').blur(function() { 
	 
	    $(".error").hide();
	    var hasError = false;
	    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	 
	    var emailaddressVal = $("#Name").val();
	    if(emailaddressVal == '') {
	      $("#Name").after('<span class="error">Please enter your Name.</span>');
	      hasError = true;
	    }
	 
	    else if(emailReg.test(emailaddressVal)) {
	      $("#Name").after('<span class="error">No Email addresses allowed</span>');
	      hasError = true;
	    }
	 
	    if(hasError == true) { return false; }
	 
	    });
	});
  </script>
</head>
<body>
  <form method="post" name="form1" action="">
	  <fieldset>
	   <label style="display:inline-block;width:100px;">Email Address:</label>
	   <input type="text" name="Name" id="Name" value="" size="32" />
	   <br /><br />
	   <label style="display:inline-block;width:100px;">Address:</label>
	   <input type="text" name="" id="Address" value="" size="32" />
	   <br /><br />
	   <input type="submit" value="Submit" id="btn-submit" />
	  </fieldset>
	</form>
</body>
</html>
Random Solutions  
 
programming4us programming4us