Unknown Person Unknown Person - 4 months ago 18
Javascript Question

How do allow comma in input text using javascript

I want user to allow comma and space too inside text field here is my code snnipet

function keywordValidation(e) {
var k;
document.all ? k = e.keyCode : k = e.which;
return ((k > 64 && k < 91) || (k > 96 && k < 123) || (k == 188) || (k >= 48 && k <= 57));
}

<input type="text" id="##" class="#" name="#" name="xyz" onkeypress="return keywordValidation(event);" />


but it restricts user to input comma and space , however it restricts special characters which is requirement.

Answer

You could try simple code

 <input type="text" onkeypress="return (event.charCode === 0 )||  /[a-zA-Z0-9,_ ]/.test(String.fromCharCode(event.charCode));"/>