Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 Limited Edition and Dev Alpha C
My Carrier: Movistar-Venezuela

Randon Number [JavaScrip + QML]

[ Edited ]

I want to use the following JavaScript code to generate random numbers, and so obtain a password.

I use a Button and TextField, but not display anything in TextField, I could help modify the code.

 

 

import bb.cascades 1.0

Page {
    Container {
        Button {
            id: ramdon
            text: "Button 1"
            onClicked: {
function randomPassword(length)
{
	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	pass = "";

	for(x=0;x<length;x++)
	{
		i = Math.floor(Math.random() * 62);
		pass += chars.charAt(i);
	}
	
	return pass;
}

function formSubmit()
{
	passform.passbox.value = randomPassword(passform.length.value);
	return false;
}
}
}
        Label {
            id: label18
            text: randomstring.text
       }
    }

}

 

 

this code complete in HTM is 

 

<html>
<head>
<title>Random Password Generator</title>
<style>
a:visited{color:black;font-family:verdana}
a:link{color:black;font-family:verdana}
a:hover{color:blue;font-family:verdana}

td {color:black;font-family:verdana;font size:8pt}
p {color:black;font-family:verdana;font size:8pt;text-decoration: none}
h1 {color:black;font-family:verdana;font size:12pt;text-decoration: none}
</style>
</head>
<body>
<script>
function randomPassword(length)
{
	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	pass = "";

	for(x=0;x<length;x++)
	{
		i = Math.floor(Math.random() * 62);
		pass += chars.charAt(i);
	}
	
	return pass;
}

function formSubmit()
{
	passform.passbox.value = randomPassword(passform.length.value);
	return false;
}

</script>
<h1 align="center">Random Password Generator</h1>

<table align="center" border="1" cellpadding="10">
<tr><td valign="middle" align="center">
<form name="passform">
<p>Length:
<select name="length">
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
</select></p>
<p>Password:
<input name="passbox" type="text" size="50" tabindex="1"></p>
<p><input type="button" value="Generate" onClick="javascript&colon;formSubmit()" tabindex="2">&nbsp;<input type="reset" value="Clear" tabindex="3">
</p></form></td></tr></table>


</body>
</html>

 


Follow me in Twitter:

@Marcossit

BBM Channel:

MarcossitDev: C0002C366  

DeviceSuite For BlackBerry 10:

Link to Download DeviceSuite:

Support was successful? Accept as Solution and click on:


Developer
Posts: 426
Registered: ‎04-10-2011
My Device: Z30

Re: Randon Number [JavaScrip + QML]

You're almost there.

Move the functions (randomPassword and formSubmit) outside the onClicked: {} slot and instead just call the formSubmit function as usual in the onClicked slot. Done.

-----------------------------------------------------------------------------
Check out my apps in the BlackBerry World
Visit my developer blog alopix.net
BBM Channel: C0047B612
Developer
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 Limited Edition and Dev Alpha C
My Carrier: Movistar-Venezuela

Re: Randon Number [JavaScrip + QML]

This My Code but not found

 


 

Page {
    Container {
        id: root
        Button {
            id: ramdon
            text: "Button 1"
            onClicked: {
            root.randomPassword(10);    

}
}
        Label {
        id: textFieldID 
        text: "Passwod: "

                                          
       }
     
    }
function randomPassword(length)
{
   chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
   pass = "";

   for(x=0;x<length;x++)
   {
      i = Math.floor(Math.random() * 62);
      pass += chars.charAt(i);
   }
   textFieldID.text = pass
   return pass;
  
}  
  
}

 

 no invoke the button, the javascript function

 


Follow me in Twitter:

@Marcossit

BBM Channel:

MarcossitDev: C0002C366  

DeviceSuite For BlackBerry 10:

Link to Download DeviceSuite:

Support was successful? Accept as Solution and click on:


Developer
Posts: 74
Registered: ‎05-31-2013
My Device: BlackBerry Z30

Re: Randon Number [JavaScrip + QML]

looks to me randomPassword() is in your Page, not Container (I may be counting the {} wrong though

If that's the case, then there is no function called RandomPassword() in "root" since "root" is the container.


Like this answer? Give it a
Visit my blog for programming help, guides and common issues.
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Randon Number [JavaScrip + QML]

try this:

    Container {
        id: root
        layout: StackLayout {
        }

        Button {
            id: ramdon
            text: "Button 1"
            onClicked: {
                root.randomPassword(10);

            }
        }

        Label {
            id: textFieldID
            text: "Passwod: "

        }

        function randomPassword(length) {
            var charArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            var pass = "";

            for (var x = 0; x < length; x ++) {
                var i = Math.floor(Math.random() * 62);
                pass += charArray.charAt(i);
            }
            textFieldID.text = pass

        }

    }

 

Developer
Posts: 188
Registered: ‎07-19-2012
My Device: Z10 Limited Edition and Dev Alpha C
My Carrier: Movistar-Venezuela

Re: Randon Number [JavaScrip + QML]

                            DropDown {
                                id: validity
                                preferredWidth: 520
                                horizontalAlignment: HorizontalAlignment.Center
                                options: [
                                    Option {
                                        text: "5"
                                        value: ""
                                        selected: true
                                    },
                                    Option {
                                        text: "6"
                                       },
                                    Option {
                                        text: "7"

                                    },
                                    Option {
                                        text: "8"
                                    },
                                    Option {
                                        text: "9"
                                    },
                                    Option {
                                        text: "10 Days"
                                    },
                                    Option {
                                        text: "11 Days"
                                    },
                                    Option {
                                        text: "12"
                                    },
                                    Option {
                                        text: "13"
                                    },
                                    Option {
                                        text: "14"
                                    },
                                    Option {
                                        text: "15"
                                    }
                                ]
                                onSelectedValueChanged: {
                                      root.randomPassword();
                                }
                            }

 

Perfect thank you .... It worked

And if by a DropDown want to add the number of characters to show how I include?


Follow me in Twitter:

@Marcossit

BBM Channel:

MarcossitDev: C0002C366  

DeviceSuite For BlackBerry 10:

Link to Download DeviceSuite:

Support was successful? Accept as Solution and click on:


Developer
Posts: 74
Registered: ‎05-31-2013
My Device: BlackBerry Z30

Re: Randon Number [JavaScrip + QML]


root.randomNumber( parseInt(validity.text); );

This should work

 



Like this answer? Give it a
Visit my blog for programming help, guides and common issues.
Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Randon Number [JavaScrip + QML]

In that case selectedOption has  the Option selected so you have to get its valur or maybe text could be fine too and pass it to your javascript function

 

Developer
Posts: 109
Registered: ‎08-23-2012
My Device: Blackberry Z10
My Carrier: TATA DOCOMO

Re: Randon Number [JavaScrip + QML]

[ Edited ]

In case of DropDown you need to do like below

 

onSelectedValueChanged: {
root.randomPassword(parseInt(validity.selectedOption.text));
}

 

This will definitely work.

 

Press like if this solve your problem and also mark as solved.

Dharmender Singh
Developer
Posts: 74
Registered: ‎05-31-2013
My Device: BlackBerry Z30

Re: Randon Number [JavaScrip + QML]

According to this link, javascript should just get the numbers from 10 days.

http://www.w3schools.com/jsref/jsref_parseint.asp


Like this answer? Give it a
Visit my blog for programming help, guides and common issues.