Validating an email address on key-press using javascript & jquery [closed]
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this questionI'm trying to prevent users from inputting invalid characters when typing-in an email address. I am NOT trying to validate the entire value as-a-whole (that happens later).
I "thought" this was the correct way to prevent a 开发者_JAVA百科list of characters (such as # or $):
/[A-Z0-9a-z@]^[$#<>?]/
This part works:
/[A-Z0-9a-z@]/
This part fails:
/^[$#<>?]/
Any thoughts?
Try /[^$#<>?]/
instead.
The ^
inside and at directly after the opening bracket makes the class negative.
You can learn more about this on regular-expressions.info
Or better, heres a workign simple regex that does it, using yours as a start.
http://jsfiddle.net/hG99U/1/
$('input[type=email]').on('keypress', function (e) {
var re = /[A-Z0-9a-z@\._]/.test(e.key);
if (!re) {
return false;
}
});
You don't need to write all the special characters into the regular expression that need to be prohibited, because there will be a large list, simple write the ones that are allowed because they are less(that you have already done and perhaps only one or two more characters viz. DOT,UnderScore
are remaining).
The code you have used to test the validity of inputted character is already fine, which will verify the character to be out of [A-Z0-9a-z@]
only, probably you can make it [A-Z0-9a-z@\._]
for other two characters.
keychar = String.fromCharCode(keynum);
regEx = /[A-Z0-9a-z@]/;
return regEx.test(keychar);
<script type="text/javascript">
<!--
var email='1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.@_'
var bksp = 'backspace'
var alt = 'alt'
function alpha(e,allow) {
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
return (allow.indexOf(String.fromCharCode(k))!=-1);
}
// -->
</script>
<input type="text" onkeypress="return alpha(event,email+bksp+alt)" />
FIXED:
The problem was some keynum values collide with undesirable characters. This fixed it!
// **********************
// .emailOnly
// Use -
// Makes the element email-only.
//
// Example -
// $('.myElement').emailOnly();
// **********************
(function($) {
$.fn.extend({
emailOnly: function() {
return this.each(function() {
return $(this).keypress(function(e, text) {
var keynum;
var keychar;
var regEx;
var allowedKeyNums = [8, 9, 35, 36, 46]; // Backspace, Tab, End, Home, (Delete & period)
if (window.event) // IE
keynum = e.keyCode;
else if (e.which) // Netscape/Firefox/Opera
keynum = e.which;
else
keynum = e.keyCode
keychar = String.fromCharCode(keynum);
regEx = /[^$#<>?]/ // Undesirable characters
// Test for keynum values that collide with undesirable characters
if ($.inArray(keynum, allowedKeyNums) > -1)
return regEx.test(keychar);
regEx = /[A-Z0-9a-z@]/
return regEx.test(keychar);
});
});
}
});
})(jQuery);
精彩评论