Sometimes we ask our users to type in something and we only accept alphabet numbers and/or letters, so they should not type something like #$%!@$&%(…
So, first we need a variable that holds user’s text. For example, “userText” with value of an input box containing user’s text. Then, we examine that variable, does it contain anything but letters and numbers or not.
Here is the codes:
var userText = "blablabla";
if(validateText(userText))
alert("Accepted!");
else
alert("You should only type letters and/or numbers...");
function validateText(text){
var letters = /^[A-Za-z0-9]+$/;
if(text.match(letters)) return true;
else return false;
}
Also watch this demonstration video to see how it works: