Using the function of regular expression, in addition to the correct conversion to the capital form of the renminbi, you can also determine whether the form of input is correct and effective.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" > < HTML xmlns= "http://www.w3.org/1999/xhtml" lang= "gb2312" > <pead> <title> convert RMB numbers to uppercase </title> <script language= "JScript" > Function convertcurrency (currencydigits) {//Constants:var Maximum_number = 99 999999999.99; Predefine the radix characters and currency symbols for output:var Cn_zero = "0"; var cn_one = "one"; var cn_two = "II"; var cn_three = "three"; var cn_four = "Restaurant"; var cn_five = "WU"; var cn_six = "Lu"; var cn_seven = "Qi"; var cn_eight = "ba"; var cn_nine = "Nine"; var Cn_ten = "Pick up"; var cn_hundred = "Bai"; var Cn_thousand = "Qian"; var cn_ten_thousand = "million"; var cn_hundred_million = "billion"; var cn_symbol = "RMB"; var cn_dollar = "Yuan"; var cn_ten_cent = "Corner"; var cn_cent = "min"; var Cn_integer = "whole"; Variables:var Integral; represent integral part of digit Number. var decimal; represent decimal part of digit number. var outputcharacters; The output result. var parts; var digits, radices, bigradices, decimals; var Zerocount; var i, p, D; var quotient, modulus; Validate Input string:currencydigits = currencydigits.tostring (); if (currencydigits = = "") {alert ("Empty input!"); Return ""; } if (Currencydigits.match (/[^,.\d]/)!= null) {alert ("Invalid characters in the input string!"); Return ""; The If ((CurrencyDigits). Match (/^ (\d{1,3} (, \d{3}) * (.) ( (\d{3},) *\d{1,3})?) | (\d+ (. \d+)?)) $/) = = null) {alert ("illegal format of digit number!"); Return ""; }//Normalize the format of input digits:currencydigits = Currencydigits.replace (/,/g, ""); Remove comma delimiters. CurrencyDigits = Currencydigits.replace (/^0+/, ""); Trim Zeros at the beginning. Assert the number is not greater than the maximum number. if (number (currencydigits) > MAXIMUM_number) {alert ("Too large a number to convert!"); Return ""; }//Process the coversion from currency digits to characters://separate integral and decimal parts before Essing coversion:parts = Currencydigits.split ("."); if (Parts.length > 1) {integral = parts[0]; decimal = parts[1]; Cut down redundant decimal digits this are after the second. decimal = Decimal.substr (0, 2); else {integral = parts[0]; decimal = ""; }//Prepare the characters corresponding to the digits:digits = new Array (Cn_zero, Cn_one, Cn_two, Cn_three, CN_FO UR, Cn_five, Cn_six, Cn_seven, Cn_eight, cn_nine); radices = new Array ("", Cn_ten, cn_hundred, Cn_thousand); Bigradices = new Array ("", Cn_ten_thousand, cn_hundred_million); decimals = new Array (cn_ten_cent, cn_cent); Start processing:outputcharacters = ""; Process integral part if it is larger than 0:if (number (integral) > 0) {zerocount = 0; for (i = 0; i < integral.length; i++) {p = integral.length-i-1; D = Integral.substr (i, 1); quotient = P/4; modulus = p% 4; if (d = = "0") {zerocount++; else {if (Zerocount > 0) {outputcharacters + = digits[0]; } zerocount = 0; Outputcharacters + = Digits[number (d)] + radices[modulus]; } if (modulus = = 0 && Zerocount < 4) {outputcharacters + = bigradices[quotient]; } outputcharacters + = Cn_dollar; }//Process decimal Part if there is:if (decimal!= "") {for (i = 0; i < decimal.length; i++) {d = decimal.substr (i, 1); if (d!= "0") {outputcharacters + = Digits[number (d)] + decimals[i]; }}//Confirm and return the final output string:if (outputcharacters = "") {outputcharacters = Cn_ze RO + cn_dollar; } if (decimal = = "") {outputcharacters + = Cn_integer; } outputcharacters = Cn_symbol + outputcharacters; return outputcharacters; } ≪/script> </pead> <body> <input id= "Digits" type= "text" name= "Digits" size= ""/> <i Nput id= "Convert" type= "button" value= "Convert" name= "Convert" onclick= "Result.value = ConvertCurrency (digits.value);" /> <input id= "result" type= "text" name= "result" size= "a"/> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]