Alphanumeric validation in javascript

Javascript Code

<input type="text" name="username" id="username" placeholder="Enter UserName" >
<script>
	$(document).ready(function(){
        $("#username").keypress(function(e){
            var charCode = e.which;
            /*
              8 - (backspace)
              32 - (space)
              65-98 - (a-z)Alphabet
            */
            if(!(charCode >= 65 && charCode <= 90)
                && !(charCode >= 97 && charCode <= 122)
                && (charCode != 32 && charCode != 8)
                && !(charCode == 9) && (charCode < 48 || charCode > 57)
            ) {
                return false;
            }
        });
    });
</script>

0 Comments

Leave a Reply

You must be logged in to post a comment.