Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

JavaScript codes to only accept alphabet letters and numbers from your users

Posted on April 27, 2018August 4, 2020 by Habibie

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:

Post Views: 354
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Hover Reveal script for Unity to show and hide object on mouse hover
  • How to Prevent UI Clicks in Unity from “Bleeding Through” to 3D Objects Behind Them
  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
© 2026 ThirteeNov | Powered by Superbs Personal Blog theme