How to Generate Random String in Javascript?
In this example, i will show you how to generate random string or characters in javascript. We can simply generate random alphanumeric string for token or etc in jquery.
As we know jquery or javascript does not have default random string generate function so we need to create custom js function to create random and unique string in javascript. we will pass length of string and it will return random string. we will use Math.random() function too.
You need to see bellow example, you can also run bellow html file and see the results. You can also check demo.
Example:
<html>
<head>
<title>How to generate random string in javascript? - ItSolutionStuff.com</title>
</head>
<body>
<input type="button" value="Create Random String" onClick="alert(generateRandomString(10))">
</body>
<script type="text/javascript">
function generateRandomString(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
</script>
</html>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Last Element from Array using JQuery?
- How to Check Array Is Empty Or Null In Javascript?
- JQuery Rotate Image Animation Example Code
- How to Remove Empty or Null Values from Array in JQuery?
- JQuery - How to Push Specific Key and Value in Array?
- How to Set Maxlength for Textarea in Javascript?
- How to Get First Element of Array in Javascript?
- How to Remove Query String from URL using JQuery?
- Automatically Scroll to Bottom of Div JQuery Example
- Check and Uncheck All Checkbox using JQuery Example
- How to Allow Only One Checkbox Checked at a Time in JQuery?
- How to Convert Line Breaks to br in jQuery ?
- How to Check Object is Empty or Not in JQuery?