How to Generate Random number on Page load using jquery ?


How to Generate Random Numbers on Every Time page loads using Jquery ?

In my previous post I had shown you “Cross site ajax call”. Today I have an interesting topic. I am going to write about "Generating random numbers using jquery on everytime page reload."

Load jquery.js file into your webpage and paste below code into the script tag.

 


<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>

<script type="text/javascript">

$(function () {
    
     var numMin =  '345';
     var numMax = '26654';
     
     var adjustedHigh = (parseFloat(numMax) - parseFloat(numMin)) + 1;
     
     
     var numRand = Math.floor(Math.random() * adjustedHigh) + parseFloat(numMin);
     
     
     if ((IsNumeric(numMin)  && (IsNumeric(numMax)) && (parseFloat(numMin) <= parseFloat(numMax )) && (numMin != '') && (numMax != ''))) {
         $("#randomnumber").val('DTKT'+numRand);
     }
});

function IsNumeric(n){
    return !isNaN(n);
}

</script>


<input type="text" name="txtTicketId" id="randomnumber" />

That's it you are done. Now every time you reload your page, you will get random generated number concatenate with string. You also can set your min and max range for random numbers, with variables numMin and numMax.

Hope this post is helpful to you. Keep visiting and if it is helpful to you then please do share with your friends and also you can join our technical network in facebook, Linkedin etc. with the link available bottom of the site.

Thank you all. All the best.

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions