jQuery get invalide character from string

Example given for jQuery get invalid character from string, JavaScript get invalid character from string, validation – jQuery Check username for invalid characters, check if string contains anything other than alphanumeric, String contains an invalid character, How to check if string contains special characters, jquery form validation.

Suppose in register form when user enter invalid character by validation we display only error message “Invalid username” or other message. In place of simple message if we display entered invalid character to user, it will help user to fix error more quickly.

Example for get invalid character from string is given in two styles, first one with normal function and second with String prototype.

jQuery get invalide character from string example: 1


Invalid character detail

Invalid chars:

<script>

function getInvalidChars(str){
   return str.replace(/[a-zA-Z0-9]/g, '');
}

//HOW TO GET INVALID CHARS
displayDetail('YOUR STRING HERE');
alert(invalidChar);

</script>

jQuery get invalide character from string example: 2


Invalid character detail

Invalid chars:

<script>

String.prototype.invalidChar = function() {
   return this.replace(/[a-zA-Z0-9]/g, '');
}

//HOW TO GET INVALID CHARS
var inputVal = $('#username').val();
var invalidChar = inputVal.invalidChar();
alert(invalidChar);

</script>