Javascript Generate Random String Example

Jul 12, 2021 . Admin

Hello Friends, Now let's see example of how to use javascript generate random string example. We will use how to use if generate random string in javascript. Here you will learn how to use javascript generate random string. This is a short guide on generate random string. Let's get started with how to generate random string in javascript. Here i will give you many example how you can check javascript generate random string. Example: 1
<html>
	<head> 
		<title>JavaScript Generate Random String</title>
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	</head>
	<body>
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h4>JavaScript Generate Random Number</h4>
						</div>
						<div class="card-body">
							<form>
								<input type="button" class="btn btn-primary" value="Generate Random String" onClick="funcstr();"><br><br>
								<b><p id="randomstr"> </p></b>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			function funcstr() {
				var characters = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
				var lenString = 7;
				var funcstr = '';
				for (var i=0; i<lenString; i++) {
					var strnum = Math.floor(Math.random() * characters.length);
					funcstr += characters.substring(strnum, strnum+1);
				}
				document.getElementById("randomstr").innerHTML = funcstr;
			}
		</script>
	</body>
</html>
Output:
Product Name:
dAMcVtf (Random String)
Example: 2
<html>
	<head> 
		<title>JavaScript Generate Random String</title>
		<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	</head>
	<body>
		<div class="container mt-5">
			<div class="row">
				<div class="col-md-6 offset-md-3">
					<div class="card">
						<div class="card-header">
							<h4>JavaScript Generate Random Number</h4>
						</div>
						<div class="card-body">
							<form name="randform">
								<input type="button" class="btn btn-primary" value="Generate Alpha-numeric String" onClick="randomStr();">
								<br><br>
								<h4 id="randomfld"></h4>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script>
			function randomStr() {
				var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
				var string_length = 8;
				var randomstring = '';
				for (var i=0; i<string_length; i++) {
					var strnum = Math.floor(Math.random() * chars.length);
					randomstring += chars.substring(strnum,strnum+1);
				}
				document.getElementById("randomfld").innerHTML = randomstring;
			}
		</script>
	</body>
</html>
Output:
C1V2AGGk (Random String)

It will help you....

#Javascript