Avoid tragedy JavaScript Password generator

Source: Internet
Author: User
Tags array sort

I have prepared a password generation tool consisting of 0-9 and a-k English letters. The numbers are in disorder. Replace a random number with a letter to generate a password of 10.

 
 
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
  2. Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
  3. <Html xmlns = "http://www.w3.org/1999/xhtml">
  4. <Head>
  5. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  6. <Title> </title>
  7. </Head>
  8. <Body>
  9. <Div>
  10. <Input id = "strat" type = "button" value = "start"/>
  11. <Textarea rows = "10" cols = "25" id = "password"> </textarea>
  12. </Div>
  13. <Script type = "text/javascript">
  14. Void function (window, doc, undefined ){
  15. Var array = [], text = 'abcdefghjk ', key, password = '', btn = doc. getElementById ('strat'), textarea = doc. getElementById ('Password ');
  16. Function unorderedArray (array ){
  17. If ({}). toString. call (array). indexOf ('array') ===-1) return;
  18. Var arr = [], value = array. toString ();
  19. Arr = array. sort (function () {return Math. random ()> 0.5? -1:1 ;});
  20. (Arr. toString () === value) & arguments. callee (array );
  21. Return arr;
  22. }
  23. Btn. onclick = function (){
  24. Key = Math. random (). toFixed (1) * 10-1;
  25. Array = [,];
  26. Password = unorderedArray (array );
  27. Password [key] = text [key];
  28. Textarea. innerHTML = password. join ('');
  29. }
  30. } (Window, document );
  31. // Console. log (unorderedArray ());
  32. </Script>
  33. </Body>
  34. </Html>

How to sort arrays in disorder is a poor math. After searching, input such a function to the array sort.

Sort is really powerful.

 
 
  1. function unorderedArray(array){  
  2.         if(({}).toString.call(array).indexOf('Array')===-1) return;  
  3.         var arr=[],value=array.toString();  
  4.         arr=array.sort(function(){return Math.random()>0.5?-1:1;});  
  5.         (arr.toString()===value) && arguments.callee(array);  
  6.         return arr;  

When using this method, we should avoid the generation of arrays, which is the same as the input array. This is a waste of effort, because the array is of a complex type and all modifications will change. Therefore, a variable is used to cache the value of the input array = array. toString (), and then the value of the array I generated is compared if it is relatively recursive. Ensure that the generated array is not sequential.

Use the following code to generate a random number 0-9.

 
 
  1. Math.random().toFixed(1) * 10-1; 

Use this serial number to get a letter from a-k, and use this letter to replace the numbers at the corresponding position of the serial number in the array.

Concatenate the obtained Array Using the join method of the array

 
 
  1. password.join(''); 

Obtain a key.

Original article: http://www.cnblogs.com/enix/archive/2011/12/22/2297913.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.