Friday, April 18, 2014

Generate random string using Javascript

Hi friends.In this article i am going to show how to create random string using javascript

Using Onclick() event i have created random string.

Description:

Onclick event when user click an event or button.



HTML Code

  <table>
      <form id="form" action="#"  method="post" name="randform">
        <tr>
          <td class="text" height="34">Coupon Code <span style="color:#FF0000">*</span></td>
          <td>
            <input type="text" name="Coupon" required class="t-box1"/>
            &nbsp;
            <input type="button" value="Genereate Code" id="generate" onClick="randomString();" class="btn btn-primary"/></td>
        </tr>
      </form>
    </table>

Jquery Code

 <script language="javascript" type="text/javascript">
function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
document.randform.Coupon.value = randomstring;
}
</script>

Please share your comments and feedback here.Thanks..!

No comments:

Post a Comment