<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>function | Code and unicorns</title><link>https://www.codeandunicorns.com/tag/function/</link><atom:link href="https://www.codeandunicorns.com/tag/function/index.xml" rel="self" type="application/rss+xml"/><description>function</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Mon, 05 Nov 2012 13:01:04 +0000</lastBuildDate><image><url>https://www.codeandunicorns.com/media/icon_hucc11e6900960b53356ccbacbee54fd10_202121_512x512_fill_lanczos_center_3.png</url><title>function</title><link>https://www.codeandunicorns.com/tag/function/</link></image><item><title>Format € money snippet</title><link>https://www.codeandunicorns.com/post/posts/format-e-money-snippet/</link><pubDate>Mon, 05 Nov 2012 13:01:04 +0000</pubDate><guid>https://www.codeandunicorns.com/post/posts/format-e-money-snippet/</guid><description>&lt;p>The following function transforms the inputed numeric value for example&lt;br>
var sum = 50.3&lt;br>
sum.formatMoney(2,’,’ , ‘.’) + “€”&lt;br>
and the output of this function is then 50,30€. As you notice it is quite simple to change it in $ or anything you desire.&lt;/p>
&lt;pre>&lt;code>&amp;lt;pre class=&amp;quot;brush: jscript; title: ; notranslate&amp;quot; title=&amp;quot;&amp;quot;&amp;gt;Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? &amp;quot;,&amp;quot; : d, t = t == undefined ? &amp;quot;.&amp;quot; : t, s = n &amp;amp;lt; 0 ? &amp;quot;-&amp;quot; : &amp;quot;&amp;quot;, i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + &amp;quot;&amp;quot;, j = (j = i.length) &amp;amp;gt; 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : &amp;quot;&amp;quot;) + i.substr(j).replace(/(\d{3})(?=\d)/g, &amp;quot;$1&amp;quot; + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : &amp;quot;&amp;quot;);
};
&amp;lt;/pre&amp;gt;
&lt;/code>&lt;/pre></description></item><item><title>Correct width in all browsers (JS snippet)</title><link>https://www.codeandunicorns.com/post/posts/width-in-browser-js-snippet/</link><pubDate>Sun, 04 Nov 2012 17:24:53 +0000</pubDate><guid>https://www.codeandunicorns.com/post/posts/width-in-browser-js-snippet/</guid><description>&lt;p>This is a short JS snippet which is quite hard to find but it works like a charm.&lt;br>
Basically with help of this JS snippet you get your width of the page correctly&lt;br>
displayed in old IE6 and IE4 if you need to make it compatible with them or&lt;br>
android/chrome/other browsers.&lt;/p>
&lt;pre>&lt;code>&amp;lt;div class=&amp;quot;fusion-fullwidth fullwidth-box hundred-percent-fullwidth&amp;quot; style='background-color: #ffffff;background-position: center center;background-repeat: no-repeat;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;'&amp;gt;
&amp;lt;div class=&amp;quot;fusion-builder-row fusion-row &amp;quot;&amp;gt;
&amp;lt;div class=&amp;quot;fusion-layout-column fusion_builder_column fusion_builder_column_1_1 fusion-one-full fusion-column-first fusion-column-last fusion-column-no-min-height 1_1&amp;quot; style='margin-top:0px;margin-bottom:0px;'&amp;gt;
&amp;lt;div class=&amp;quot;fusion-column-wrapper&amp;quot; style=&amp;quot;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;&amp;quot; data-bg-url=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;brush: jscript; title: ; notranslate&amp;quot; title=&amp;quot;&amp;quot;&amp;gt;
function get_width() {
var myWidth = 0;
if(document.width) {
//chrome
myWidth = document.width;
} else if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
} else if( document.documentElement &amp;amp;&amp;amp; ( document.documentElement.clientWidth) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
} else if( document.body &amp;amp;&amp;amp; ( document.body.clientWidth ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
}
return myWidth;
}
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
And here is another JS snippet which does the same super compatibility except with height now width.
&amp;lt;/p&amp;gt;
&amp;lt;div class=&amp;quot;fusion-clearfix&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;fusion-layout-column fusion_builder_column fusion_builder_column_1_1 fusion-one-full fusion-column-first fusion-column-last fusion-column-no-min-height 1_1&amp;quot; style='margin-top:0px;margin-bottom:0px;'&amp;gt;
&amp;lt;div class=&amp;quot;fusion-column-wrapper&amp;quot; style=&amp;quot;background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;&amp;quot; data-bg-url=&amp;quot;&amp;quot;&amp;gt;
&amp;lt;pre class=&amp;quot;brush: jscript; title: ; notranslate&amp;quot; title=&amp;quot;&amp;quot;&amp;gt;
function get_height() {
var myHeight = 0;
if(document.height) {
//chrome
myHeight = document.height;
} else if( typeof( window.innerHeight ) == 'number' ) {
//Non-IE
myHeight = window.innerHeight;
} else if(document.documentElement &amp;amp;&amp;amp; (document.documentElement.clientHeight)) {
//IE 6+ in 'standards compliant mode'
myHeight = document.documentElement.clientHeight;
} else if(document.body &amp;amp;&amp;amp; (document.body.clientHeight)) {
//IE 4 compatible
myHeight = document.body.clientHeight;
}
return myHeight;
}
&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;
This JS snippets can be quite hard to find so hopefully this helps people who are searching for something like this.
&amp;lt;div class=&amp;quot;fusion-clearfix&amp;quot;&amp;gt;
&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code>&lt;/pre></description></item><item><title>3 Data validation methods in javascript</title><link>https://www.codeandunicorns.com/post/posts/data-validation-method/</link><pubDate>Wed, 31 Oct 2012 20:12:09 +0000</pubDate><guid>https://www.codeandunicorns.com/post/posts/data-validation-method/</guid><description>&lt;p>Function is isValideDate can be used for example on a string data.isValideDate() to confirm that date is in correct format like these 2 for example:&lt;/p>
&lt;ul>
&lt;li>04-11-2012&lt;/li>
&lt;li>4-11-12&lt;/li>
&lt;/ul>
&lt;p>The isNumeric function is used same way with data.isNumeric() on a string and the function is used validation if the whole input is numeric&lt;/p>
&lt;pre>&lt;code>&amp;lt;pre class=&amp;quot;brush: jscript; title: ; notranslate&amp;quot; title=&amp;quot;&amp;quot;&amp;gt;String.prototype.isValidDate = function()
{
var IsoDateRe = new RegExp(&amp;quot;^([0-9]{2})-([0-9]{2})-([0-9]{4})$&amp;quot;);
var matches = IsoDateRe.exec(this);
if (!matches) return false;
var composedDate = new Date(matches[3], (matches[2] - 1), matches[1]);
return ((composedDate.getMonth() == (matches[2] - 1)) &amp;amp;&amp;amp;
(composedDate.getDate() == matches[1]) &amp;amp;&amp;amp;
(composedDate.getFullYear() == matches[3]));
}
String.prototype.isNumeric = function () {
var intRegex = /^\d+$/;
var v = this;
if(intRegex.test(v)) {
return true;
}
return false;
}
&amp;lt;/pre&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>And just for you, a third function for strings. This function data.isDouble() checks if the inputed data is of a double type.&lt;/p>
&lt;pre>&lt;code>&amp;lt;pre class=&amp;quot;brush: jscript; title: ; notranslate&amp;quot; title=&amp;quot;&amp;quot;&amp;gt;String.prototype.isDouble = function () {
var validChars = '0123456789.';
for(var i = 0; i &amp;amp;lt; this.length; i++) {
if(validChars.indexOf(this.charAt(i)) == -1)
return false;
}
var dblVar = parseFloat(this);
if(isNaN(dblVar))
return false;
return true;;
}
&amp;lt;/pre&amp;gt;
&lt;/code>&lt;/pre></description></item></channel></rss>