Random Password Generation
A Small Util Function to create random Password Generation https://gist.github.com/988479
Posts Tagged ‘ string ’
A Small Util Function to create random Password Generation https://gist.github.com/988479
The AS3 Util Function is posted on the below link
Utility to convert number or integer to String. The util function is useful in many applications.
https://gist.github.com/703108
The code below demonstrates the data type conversions among basic DataTypes used most in Flex. The ‘init()’ method does to and from conversion for ArrayCollection to XML and XML to ArrayCollection
Object, String, XML, ArrayCollection
import mx.collections.ArrayCollection;
import mx.rpc.xml.SimpleXMLEncoder;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.utils.ObjectUtil;
private function xmlToArrayCollection(xml:XML):ArrayCollection{
var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var resultObj:Object = decoder.decodeXML(xmlDoc);
var ac:ArrayCollection =new ArrayCollection(Array(resultObj.root.list.source.item));
return ac;
}
private function objectToXML(obj:Object):XML {
var qName:QName = new QName(“root”);
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
private function objectToArrayCollection(obj:Object):ArrayCollection {
var ac:ArrayCollection = new ArrayCollection(obj as Array);
return ac;
}
private function arrayCollectionToXML(ac:ArrayCollection):XML{
var xml:XML = objectToXML(ac);
return xml;
}
private function init():void{
var arr:Array = new Array();
arr.push({data:0,name:’deva’});
arr.push({data:1,name:’raj’});
var ac:ArrayCollection = new ArrayCollection(arr);
var xml:XML = arrayCollectionToXML(ac);
var newAc:ArrayCollection = xmlToArrayCollection(xml);
trace(newAc[0][0].name);
}
private function objToStr(value:Object,indent:int =0,
refs:Dictionary= null,
namespaceURIs:Array = null,
exclude:Array = null):String{
var str:String;
var refCount:int = 0;
if (value is Date)
{
return value.toString();
}
else if (value is XMLNode)
{
return value.toString();
}
else if (value is Class)
{
return “(” + getQualifiedClassName(value) + “)”;
}
else
{
var classInfo:Object = ObjectUtil.getClassInfo(value, exclude,
{ includeReadOnly: true, uris: namespaceURIs });
var properties:Array = classInfo.properties;
str = “(” + classInfo.name + “)”;
if (refs == null)
refs = new Dictionary(true);
var id:Object = refs[value];
if (id != null)
{
str += “#” + int(id);
return str;
}
if (value != null)
{
str += “#” + refCount.toString();
refs[value] = refCount;
refCount++;
}
var isArray:Boolean = value is Array;
var isDict:Boolean = value is Dictionary;
var prop:*;
indent += 2;
for (var j:int = 0; j < properties.length; j++)
{
str = newline(str, indent);
prop = properties[j];
if (isArray)
str += “[";
else if (isDict)
str += "{";
if (isDict)
{
str += objToStr(prop, indent, refs,
namespaceURIs, exclude);
}
else
{
str += prop.toString();
}
if (isArray)
str += "] “;
else if (isDict)
str += “} = “;
else
str += ” = “;
try
{
str += objToStr(value[prop], indent, refs,
namespaceURIs, exclude);
}
catch(e:Error)
{
str += “?”;
}
}
indent -= 2;
return str;
}
}
private static function newline(str:String, n:int = 0):String
{
var result:String = str;
result += “\n”;
for (var i:int = 0; i < n; i++)
{
result += ” “;
}
return result;
}
/* General function for Conversion (thanks, Krystian Bień)
public function xmlToArrayCollection(xml:XML):ArrayCollection{
var temp:String = ‘<items>’ + xml.toString() + ‘</items>’;
xml = XML(temp);
var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
var resultObj:Object = decoder.decodeXML(xmlDoc);
var ac:ArrayCollection;
ac = new ArrayCollection();
ac.addItem(resultObj.items);
return ac;
}*/
Some Basic String functions:
public function replaceChars(str:String, Char:String, replace:String) : String
{
var result:String = “”;
for (var i:int = 0; i < str.length; i++) {
if (str.charAt(i) == Char)
{
result += replace;
}
else
{
result += str.charAt(i)
}
}
return result;
}public function trimWhiteSpace(str:String) : String
{
var pattern:RegExp = /[\s]+/g;
return str.replace(pattern, “”);
}public function containsAlphaNumeric(str:String) : Boolean
{
var pattern:RegExp = /[^a-zA-Z0-9]/;
return str.search(pattern) != 0;
}
The below code is example for how to use MathParser.as used for evaluating maths expressions using flex. Inspired from flashmaths
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” backgroundColor=”#333333″ creationComplete=”calculate()”>
<mx:Script>
<![CDATA[
[Bindable]
private var evalStr:String;
private var mpExp:MathParser = new MathParser([ ]);
private function calculate ():void {
var compobjVal:Object = mpExp.doCompile(mathStr.text);
evalStr = String(mpExp.doEval(compobjVal.PolishArray, []));
}
]]>
</mx:Script>
<mx:TextInput id=”mathStr” text=”((7*(1-8))*9)-6+7″ change=”calculate()” />
<mx:Text id=”resVal” color=”White” text=”{evalStr}” />
</mx:Application>
The below function validates the email ID string using regular expression.
private function validateEmail(mailid:String):Boolean {
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(mailid);
}//e-mail option 2:
/[a-zA-Z0-9!$&*.=^`|~#%'+\/?_{}-]+@([\w_-]+\.)+[a-zA-Z]{2,4}/;
//URL:
/(https?|ftp|svn)(:\/\/[\w-_.:;!?~*\'()\/\@&=+\$,%#]+)/;
//Color:
/#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/;
//String Quote:
/(“|’)(.+)?\1/;
IP evaluation:
var ipPattern:RegExp = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Mar | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||