how to block special characters in javascript

Solutions on MaxInterview for how to block special characters in javascript by the best coders in the world

showing results for - "how to block special characters in javascript"
Pia
27 May 2020
1var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
2
3for (var i = 0; i < document.formname.fieldname.value.length; i++) {
4    if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
5        alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
6        return false;
7    }
8}
9
Flore
02 Aug 2018
1var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
2
3for (var i = 0; i < document.formname.fieldname.value.length; i++) {
4    if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
5        alert ("Your username has special characters. \nThese are not allowed.\n Please remove them and try again.");
6        return false;
7    }
8}