How to check input[type=text] allowed characters?

0 like 0 dislike
5 views
I want to be able to specify valid characters only
And if you enter any other character such as /, show alert "Typo / this is a forbidden symbol".
$('#js_validate').on('input', validate); function validate(e) { var $item = $(this), value = $item.val(); var st = new RegExp('^[abcdefghjklmnprstuvwxyzABCDEFGHJklmnprstuvwxyz1234567890]$'); if (st.test(value)) { // been checked - all OK return true; } else { // do not give to enter the wrong symbol e.preventDefault(); // connected script sweetalert.js // show alert that it was a typo swal ( 'Typo' , 'only letters and numbers' , 'error' ); } }

Help to correct the code. https://codepen.io/anon/pen/ZrRdPZ
by | 5 views

1 Answer

0 like 0 dislike
var st = new RegExp('[^a-zA-Z0-9]+');
and change the condition to if (!st.test(value))
by
110,608 questions
257,186 answers
0 comments
28,662 users