[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Javascript code to generate a random number

Posted on 2005-05-15
7
Medium Priority
?
692 Views
Last Modified: 2012-06-21
Requirements:
1. Scripts must work with NS4+, IE4+ and AOL most popular versions
2. When click a button name "Code Gen", it will generate a random code base on the below formular, then populate the text box with the new code, re-click "Code Gen" button will regenerate and replace the text box with the new random code.

The text box name is: name="rqaRedemptionCode"

3. Formular:

[Year Code + Month Code + Date] - [random 4 digit number]

Year Code = A to J (A=0 to J=9, if year end with 0 then A, current year is: 5 or F)
Month Code = B to M (B=1 to M=12, current month is May, then F)
Date = actual date (1 to 31)

random 4 digit number=any popular method of produce 4 digit random number

For example: today date is 2005, 5,15
example code is: FF15-5624

4. Please provide a ready to use code with html for testing.

Thanks, Maximum 500 as always.
0
Comment
Question by:andreni78
  • 4
  • 2
7 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 2000 total points
ID: 14007698
Something like this

I have tested the code in IE and Firefox. It is plain Javascript so it should work in all major browsers.


<html>
    <head>
    <script type="text/javascript">
   
    function generateCode(){
        var letters = 'ABCDEFGHIJKLM';
        var d = new Date();
        var code = letters.charAt(d.getFullYear()%10);
        code+=letters.charAt(d.getMonth()/1+1);
        code+=d.getDate();
        code+='-';
        for(var no=0;no<4;no++){
            code+=Math.floor(Math.random()*10);    
        }
        document.myForm.codeField.value = code;    
       
    }
    </script>
    </head>
    <body>
    <form name="myForm">
    <input type="text" name="codeField">
    <input type="button" value="Generate" onclick="generateCode()">
    </form>
    </body>
</html>
0
 
LVL 32

Expert Comment

by:Batalf
ID: 14007705
I forgot the name of the form field.

You can change the name in the function from

document.myForm.codeField.value = code;    

to

document.myForm.rqaRedemptionCode.value = code;    

and the text field in the form from

<input type="text" name="codeField">

to

<input type="text" name="rqaRedemptionCode">

Batalf

0
 
LVL 9

Expert Comment

by:william007
ID: 14007814
Hi!~ Batalf
just curious, I have seen you put d.getMonth()/1 not only one time...
I have tested d.getMonth() is a Number type(I only test in IE6 and firefox), what is the significant to divide by one?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Batalf
ID: 14007833
Hi william007,

It's because Javascript uses '+' both to concatinate strings and to add numbers, and because it doesn't fully support primitive variable types.

So I perform an arithmetic operation on the numbers to get the values added instead of concatinated. After checking, it doesn't look like it's needed in this example, but I have found it to be a good habit to always do it this way.

Example that illustrates it:

      <script type="text/javascript">
      var variable1 = '100';
      alert(variable1 + 5); // Alerts 1005
      alert(variable1/1 + 5); // Alerts 105
      </script>
Batalf





0
 
LVL 9

Expert Comment

by:william007
ID: 14007857
Oic, is for "security purpose":-)
0
 

Author Comment

by:andreni78
ID: 14008002
Hi Batalf,

Thank you for the superfast response. It works perfectly.

0
 
LVL 32

Expert Comment

by:Batalf
ID: 14009335
Glad I could help!

Batalf
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question