LINUXSOFT.cz Přeskoč levou lištu

ARCHIV



   

> Kontrola měny

Edituj záznam
Kategorie: Javascript
Programovací jazyk: Javascript
Domovská stránka: http://quantumsoftware.net
Download:
Tvůrce: Derek J. Klingman
Popis skriptu: Skript zkontroluje měnu zadanou do textboxu. Funguje ale pouze pro měnu dolar.
Nároky na klienta: Aktivní JS
Nároky na server: žádné
Ukázka spuštěného skriptu
Kód s komentáři:
<!-- KROK JEDNA: Vložte následujici kod do hlavičky HEAD -->



<HEAD>



<!-- Další skripty naleznete na -->

<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Original: Derek J. Klingman (dklingman@quantumsoftware.net) -->

<!-- Web Site: http://quantumsoftware.net -->

<script language="javascript">



function isCurrency(f)

{

var nNum = 0; // Total numbers for currency value.

var nDollarSign = 0; // Total times a dollar sign occurs.

var nDecimal = 0; // Total times a decimal point occurs.

var nCommas = 0; // Total times a comma occurs.

var txtLen; // Length of string passed.

var xTxt; // Assigned object passed.

var sDollarVal; // Assigned dollar amount with or without commas.

var bComma;

var decPos; // Assigned value of numbers or positions after decimal point.

var nNumCount = 0; // Total number between commas.

var i; // For forloop indexing.

var x; // Assigned each indivual character in string.



// Set the xTxt variable to the object passed to this function.

// Assign the length of the string to txtLen.

xTxt = f.txt;

txtLen = xTxt.value.length



for(i = 0; i < txtLen; i++)

{

// Assign charater in substring to x.

x = xTxt.value.substr(i, 1);





if(x == "$")

nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.

else if(x == ".")

nDecimal = nDecimal + 1; // Sum total times decimal point occurs.

else if(x == ",")

nCommas = nCommas + 1; // Sum total times comma occurs.

else if(parseInt(x) >= 0 || parseInt(x) <= 9)

nNum = nNum + 1; // If the character is a number sum total times a number occurs.

else

{

// Error occurs if any other character value is in the string

// othere then the valid characters.

alert("ERROR! \n\nYou have entered an illegal value!\nPlease enter only: Dollar" +

" Signs, Commas, Decimal Points, and numbers between 0...9!");

return false;

} // end else

} // end for



if(nDollarSign > 1)

{

alert("ERROR! \n\nYou have entered more then one dollar sign!\nPlease only enter one!");

return false;

} // end if



if(nDecimal > 1)

{

alert("ERROR! \n\nYou have entered more then one decimal point!\nPlease only enter one!");

return false;

} // end if



if(nDollarSign == 1)

{

// Make sure dollar sign in the first character in string

// if there is a dollar sign present.

if(xTxt.value.indexOf("$") != 0)

{

alert("ERROR! \n\nThe dollar sign you entered is not in the correct position!");

return false;

} // end if

}// end if



if(nDecimal == 1)

{

// Get the number of numbers after the decimal point in

// the string if there is a decimal point present

decPos = (txtLen - 1) - xTxt.value.indexOf(".");



// Floating point cannot be more then two.

// Valid format after decimal point.

/**********************************/

/* $#.##, $#.#, $.#, $#., $.## */

/**********************************/

if(decPos > 2)

{

alert("ERROR! \n\nThe decimal point you entered is not in the correct position!");

return false;

} // end if

} // end if



if(nCommas == 0)

{

// If no commas are present value is a valid US

// currency.

return true;

}

else

{

// Get total number of dollar number(s), removing

// floating point numbers or cents.

nNum = nNum - decPos;



// Determine if dollar sign is in string so to be

// removed.

// After determining dollar sign, assign sDollarVal

// numbers and comma(s)

if(xTxt.value.indexOf("$", 0) == 0)

sDollarVal = xTxt.value.substr(1, (nNum + nCommas));

else

sDollarVal = xTxt.value.substr(0, (nNum + nCommas));





// Determine if a zero is the first number or if a

// comma is the first or last character in the string.

if(sDollarVal.lastIndexOf("0", 0) == 0 )

{

alert("ERROR! \n\nYou cannot start the dollar amount out with a zero!");

return false;

}

else if(sDollarVal.lastIndexOf(",", 0) == 0)

{

alert("ERROR! \n\nYou cannot start the dollar amount out with a comma!");

return false;

}

else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1))

{

alert("ERROR! \n\nYou cannot end the dollar amount with a comma!");

return false;

}

else

{

// Initialize bComma indicating a comma has not been

// occured yet.

bComma = false;

for(i = 0; i < sDollarVal.length; i++)

{

// Assign charater in substring to x.

x = sDollarVal.substr(i, 1);



if(parseInt(x) >= 0 || parseInt(x) <= 9)

{

// If x is a number add one to the number counter.

nNumCount = nNumCount + 1;



// Sense comma(s) are present number counter cannot

// be more then three before the first or next comma.

if(nNumCount > 3)

{

alert("ERROR! \n\nYou have a mis-placed comma!");

return false;

} // end if

}

else

{

// If the number counter is less then three and

// the comma indicator is true the comma is either

// mis-placed or there are not enough values.

if(nNumCount != 3 && bComma)

{

alert("ERROR! \n\nYou have a mis-placed comma!");

return false;

} // end if



// Reset the number counter back to zero.

nNumCount = 0;



// Set the comma indicator to true indicating

// that the first comma has been found and that

// there now MUST be three numbers after each

// comma until the loop hits the end.

bComma = true;

} // end if

} // end for



// Determine if after the loop ended that there

// was a total of three final numbers after the

// last comma.

if(nNumCount != 3 && bComma)

{

alert("ERROR! \n\nYou have a mis-placed comma!");

return false;

} // end if

} // end if

} // end if



// Return true indicating that the value is a valid

// currency.

return true;

}

</script>

</HEAD>



<!-- STEP TWO: Copy this code into the BODY of your HTML document -->



<BODY>



<!-- -->

<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Original: Derek J. Klingman (dklingman@quantumsoftware.net) -->

<!-- Web Site: http://quantumsoftware.net -->

<form action="testcomplete.htm" onsubmit="return isCurrency(frm);" name="frm">

<input type="text" name="txt" size="7" value="$0.00"><p></p>

<p><input type="submit" value="Odeslat" name="B1"></p>

</form>



<!-- velikost: 6.82 KB -->
Zadal/a: Linuxák


pridej.cz

> Vyhledávání software
> Vyhledávání článků

28.11.2018 23:56 /František Kučera
Prosincový sraz spolku OpenAlt se koná ve středu 5.12.2018 od 16:00 na adrese Zikova 1903/4, Praha 6. Tentokrát navštívíme organizaci CESNET. Na programu jsou dvě přednášky: Distribuované úložiště Ceph (Michal Strnad) a Plně šifrovaný disk na moderním systému (Ondřej Caletka). Následně se přesuneme do některé z nedalekých restaurací, kde budeme pokračovat v diskusi.
Komentářů: 1

12.11.2018 21:28 /Redakce Linuxsoft.cz
22. listopadu 2018 se koná v Praze na Karlově náměstí již pátý ročník konference s tématem Datová centra pro business, která nabídne odpovědi na aktuální a často řešené otázky: Jaké jsou aktuální trendy v oblasti datových center a jak je optimálně využít pro vlastní prospěch? Jak si zajistit odpovídající služby datových center? Podle jakých kritérií vybírat dodavatele služeb? Jak volit vhodné součásti infrastruktury při budování či rozšiřování vlastního datového centra? Jak efektivně datové centrum spravovat? Jak co nejlépe eliminovat možná rizika? apod. Příznivci LinuxSoftu mohou při registraci uplatnit kód LIN350, který jim přinese zvýhodněné vstupné s 50% slevou.
Přidat komentář

6.11.2018 2:04 /František Kučera
Říjnový pražský sraz spolku OpenAlt se koná v listopadu – již tento čtvrtek – 8. 11. 2018 od 18:00 v Radegastovně Perón (Stroupežnického 20, Praha 5). Tentokrát bez oficiální přednášky, ale zato s dobrým jídlem a pivem – volná diskuse na téma umění a technologie, IoT, CNC, svobodný software, hardware a další hračky.
Přidat komentář

4.10.2018 21:30 /Ondřej Čečák
LinuxDays 2018 již tento víkend, registrace je otevřená.
Přidat komentář

18.9.2018 23:30 /František Kučera
Zářijový pražský sraz spolku OpenAlt se koná již tento čtvrtek – 20. 9. 2018 od 18:00 v Radegastovně Perón (Stroupežnického 20, Praha 5). Tentokrát bez oficiální přednášky, ale zato s dobrým jídlem a pivem – volná diskuse na téma IoT, CNC, svobodný software, hardware a další hračky.
Přidat komentář

9.9.2018 14:15 /Redakce Linuxsoft.cz
20.9.2018 proběhne v pražském Kongresovém centru Vavruška konference Mobilní řešení pro business. Návštěvníci si vyslechnou mimo jiné přednášky na témata: Nejdůležitější aktuální trendy v oblasti mobilních technologií, správa a zabezpečení mobilních zařízení ve firmách, jak mobilně přistupovat k informačnímu systému firmy, kdy se vyplatí používat odolná mobilní zařízení nebo jak zabezpečit mobilní komunikaci.
Přidat komentář

12.8.2018 16:58 /František Kučera
Srpnový pražský sraz spolku OpenAlt se koná ve čtvrtek – 16. 8. 2018 od 19:00 v Kavárně Ideál (Sázavská 30, Praha), kde máme rezervovaný salonek. Tentokrát jsou tématem srazu databáze prezentaci svého projektu si pro nás připravil Standa Dzik. Dále bude prostor, abychom probrali nápady na využití IoT a sítě The Things Network, případně další témata.
Přidat komentář

16.7.2018 1:05 /František Kučera
Červencový pražský sraz spolku OpenAlt se koná již tento čtvrtek – 19. 7. 2018 od 18:00 v Kavárně Ideál (Sázavská 30, Praha), kde máme rezervovaný salonek. Tentokrát bude přednáška na téma: automatizační nástroj Ansible, kterou si připravil Martin Vicián.
Přidat komentář

   Více ...   Přidat zprávičku

> Poslední diskuze

31.7.2023 14:13 / Linda Graham
iPhone Services

30.11.2022 9:32 / Kyle McDermott
Hosting download unavailable

13.12.2018 10:57 / Jan Mareš
Re: zavináč

2.12.2018 23:56 / František Kučera
Sraz

5.10.2018 17:12 / Jakub Kuljovsky
Re: Jaký kurz a software by jste doporučili pro začínajcího kodéra?

Více ...

ISSN 1801-3805 | Provozovatel: Pavel Kysilka, IČ: 72868490 (2003-2024) | mail at linuxsoft dot cz | Design: www.megadesign.cz | Textová verze