hi,
we want to allow only numbers and alphabets in text box on keypress event..
can some help
hi,
we want to allow only numbers and alphabets in text box on keypress event..
can some help
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Oct 19, 2023, 3:37 AM EDT
Check this to restrict the user from entering special characters and allow only numbers and alphabets in a text box on the
keypressevent:Restrict Special Characters $(document).ready(function() { $('#text-input').on('keypress', function(event) { var inputValue = event.key; var regex = /^[A-Za-z0-9]+$/; if (!regex.test(inputValue)) { event.preventDefault(); } }); });Check here the example: https://jsfiddle.net/a5sjk29L/
Vishal YelveRank #178 · 13.6K reputationPosted Oct 20, 2023, 9:13 AM EDT
Hi Murali,
Try this
$(function () { $("#txtName").keypress(function (e) { var keyCode = e.keyCode || e.which; $("#lblError").html(""); //Regex for Valid Characters i.e. Alphabets and Numbers. var regex = /^[A-Za-z0-9]+$/; //Validate TextBox value against the Regex. var isValid = regex.test(String.fromCharCode(keyCode)); if (!isValid) { $("#lblError").html("Only Alphabets and Numbers allowed."); } return isValid; }); });