thanks ghreddy but the progrm is failing in some

Results 1 to 2 of 2

Thread: thanks ghreddy but the progrm is failing in some

  1. #1
    Join Date
    Dec 1969

    Default thanks ghreddy but the progrm is failing in some

    Hi ghreddy,<BR> Your program works excellently in some conditions but it is failing when the stlen becomes -1.that is when Math.round <BR> is rounding the lena/3 to higher digit.Will you help me again to solve this problem ,as i am new to the programming.<BR>it is failing when i use 12,123.<BR>thanks<BR>jack<BR><BR> &#060;SCRIPT LANGUAGE="JavaScript"&#062;<BR> <BR> <BR> function comarepalcement(theform){<BR> fmtstring1=theform.frmtxt1.value<BR> <BR> while(fmtstring1.indexOf(",") &#062;0)<BR> {<BR> fmtstring1 = fmtstring1.replace(",","")<BR> }<BR> //var fmtstring1="100000000000";<BR> var lena=fmtstring1.length<BR> var fmtstring<BR> //alert(ret)<BR> if (parseInt(lena)&#062;3)<BR> {<BR> num=Math.round(parseInt(lena)/3)<BR> alert(num)<BR> stlen=parseInt(lena)-(num*3)<BR><BR> if (stlen == 0) {<BR> stlen = 3<BR> }<BR> fmtstring = fmtstring1.substr(0,stlen) <BR> // alert(fmtstring + " LEN " + stlen)<BR> for (i=stlen;i&#060;parseInt(lena)-1;i=i+3)<BR> {<BR> fmtstring = fmtstring + "," + fmtstring1.substr(i,3)<BR> }<BR> alert(fmtstring)<BR> }<BR> if (fmtstring==theform.frmtxt1.value) {<BR> alert ("the format is correct")<BR> return true<BR> }<BR> else {<BR> alert ("format is wrong")<BR> return false<BR> }<BR> <BR> }<BR> &#060;/SCRIPT&#062;<BR><BR><BR> &#060;FORM name=TransactionInfo method=post onsubmit="return comarepalcement(this)"&#062;<BR>&#060;input type="text" name="frmtxt1" &#062;<BR>&#060;input type="submit" name="submit1"&#062;<BR>&#060;/form&#062;

  2. #2
    Dan Evans Guest

    Default Another possibility

    This script reverses the input string, then checks every 4th character for a comma until the end of the string. The only oddity is adding a comma to the start of the string. This is because the mod function means we check indexes 0, 4, 8... which represent the 1st, 5th, 9th chars etc. The initial comma just increase string length by one so it all slots into place. It works fine for me, catching the errors I tried, but if it doesn&#039t work just say...<BR><BR>&#060;script language = "javascript"&#062;<BR>alert(validate("1,000,000")) ;<BR>function validate(str) {<BR> var str2 = ""; // We will reverse str and store it in str2, then check every 4th char for a comma<BR> for (j = str.length - 1; j &#062;=0; j--) str2 += str.charAt(j);<BR> str2 = "," + str2; // Add an initial comma.<BR> for (j = 0; j &#060; str2.length; j++) {<BR> if (j % 4 == 0) { // Check chars 0, 4, 8..., i.e., the 1st, 5th, 9th...chars <BR> if (str2.charAt(j) != ",") return false; // If it&#039s not a comma return false<BR> } else { // Just make sure the other chars are numebrs<BR> if (isNaN(str2.charAt(j))) return false; // If the char is not a number return false<BR> }<BR> }<BR> return true; // Made it all the way through, so return true<BR>}<BR>&#060;/script&#062;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts