usauae Posted June 16, 2022 Report Share Posted June 16, 2022 How do I restrict a text box not to have special characters only. ex1: S@m is ok. ex2: $&@ is not ok Quote Link to comment Share on other sites More sharing options...
Epic Posted June 16, 2022 Report Share Posted June 16, 2022 4 minutes ago, usauae said: How do I restrict a text box not to have special characters only. ex1: S@m is ok. ex2: $&@ is not ok $('input').on('keypress', function (event) { var regex = new RegExp("^[a-zA-Z0-9]+$"); var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); if (!regex.test(key)) { event.preventDefault(); return false; } }); Quote Link to comment Share on other sites More sharing options...
YOU Posted June 16, 2022 Report Share Posted June 16, 2022 Restrict to enter? Or validate on submit? Quote Link to comment Share on other sites More sharing options...
usauae Posted June 16, 2022 Author Report Share Posted June 16, 2022 1 minute ago, YOU said: Restrict to enter? Or validate on submit? Okay with either bro Quote Link to comment Share on other sites More sharing options...
usauae Posted June 16, 2022 Author Report Share Posted June 16, 2022 11 minutes ago, Epic said: $('input').on('keypress', function (event) { var regex = new RegExp("^[a-zA-Z0-9]+$"); var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); if (!regex.test(key)) { event.preventDefault(); return false; } }); Bro idi complete ga special characters block chestundi kada I should be able to enter s@m but not $@/\/\ Quote Link to comment Share on other sites More sharing options...
Epic Posted June 16, 2022 Report Share Posted June 16, 2022 11 minutes ago, usauae said: Bro idi complete ga special characters block chestundi kada I should be able to enter s@m but not $@/\/\ Oh in that case probably you can try this on submit string str = “$@/\/\”; var regex = /^[^a-zA-Z0-9]+$/; var onlySpl = regex.test(str); if (onlySpl) // show notification Quote Link to comment Share on other sites More sharing options...
usauae Posted June 16, 2022 Author Report Share Posted June 16, 2022 8 minutes ago, Epic said: Oh in that case probably you can try this on submit string str = “$@/\/\”; var regex = /^[^a-zA-Z0-9]+$/; var onlySpl = regex.test(str); if (onlySpl) // show notification worked like a charm bro. ila finger tips meeda yela vuntayi bro neeku ? naku 0 memory asalu. Quote Link to comment Share on other sites More sharing options...
Epic Posted June 16, 2022 Report Share Posted June 16, 2022 4 minutes ago, usauae said: worked like a charm bro. ila finger tips meeda yela vuntayi bro neeku ? naku 0 memory asalu. Google search 2 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.