Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random alphanumeric string in form field

    Hello everyone.

    I am completely new to javascript and am basically able to copy/paste code with some minor editing.

    What I am looking for is a way to have a 5 digit alphanumeric number generated inside a form field to be sent through php(mail).


    Any help would be greatly appreciated, and any more information I can give, I will gladly do so.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by jaggedgenius View Post
    5 digit alphanumeric
    Alphanumeric is a combination of letters AND digits. So: you need a 5 length characters alphanumeric string? Or a 5 digits number? Are letters and/or digits required?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    If you use PHP wouldn't it be better to do it in PHP?

    This function returns a lowercase alphanumeric string of specified length.
    Code:
    <script type="text/javascript">
    
    function randAlnum( len )
    {
      var str = '',
          rv;   
          
      do{
          str = "";   
          
          for( var i = 0; i < len; i++ )
          {
            rv = Math.floor( Math.random() * 36 );  
            str += String.fromCharCode( rv + ( rv < 10 ? 48 : 87 ) );
          }
             
        }while( !/[a-z]/.test(str) || !/[0-9]/.test(str) );
      
      return str;
    }
    Last edited by Logic Ali; 10-12-2011 at 01:01 PM.

  • #4
    Regular Coder
    Join Date
    Jun 2011
    Posts
    119
    Thanks
    43
    Thanked 0 Times in 0 Posts
    Hi Ali,
    Nice Answer.
    But what does this code do-
    Code:
     !/[a-z]/.test(str) || !/[0-9]/.test(str)
    Is "test" an in-built function?

    Can you please explain the logic of the above code? will be greatly benefited
    Also why such a complicated operation to generate a alphanumeric code ?
    Code:
    rv = Math.floor( Math.random() * 36 );  
            str += String.fromCharCode( rv + ( rv < 10 ? 48 : 87 ) );
    I don't understand why you require this codes -
    Code:
    str += String.fromCharCode( rv + ( rv < 10 ? 48 : 87 ) );
    Can you plz explain?

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by ippo View Post
    Hi Ali,
    Nice Answer.
    But what does this code do-
    Code:
     !/[a-z]/.test(str) || !/[0-9]/.test(str)
    Is "test" an in-built function?

    Can you please explain the logic of the above code? will be greatly benefited
    Also why such a complicated operation to generate a alphanumeric code ?
    To appreciate what's required, why don't you make your own attempt?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •