C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Javascript »

Number validation with digit grouping in Javascript


Posted Date: 10 Nov 2008    Resource Type: Code Snippets    Category: Javascript
Author: Saravanan KanagarasuMember Level: Gold    
Rating: 1 out of 5Points: 10



The following code is used for validating the numbers with digit grouping(ie. Convert the digits into like Currency $) using Javascript.
For ex,
1. If you given the input 20000, it generates the output $20,000.00
2. If you given the input 2555, it generates the output $2,555.00
And also it is having some validation, whether if the input is number or not



function digitGrouping(digit) {
var obj = document.getElementById(digit);
var digit = document.getElementById(digit).value;
var isGroup = "1234567890,.$";
var chk;
for(var i = 0; i <= digit.length-1; i++) {
if(isGroup.indexOf(digit.substring(i, i+1)) != -1) {
chk = true;
} else {
chk = false;
alert("Not a valid number \nNumber format should be like this 1,000,000.00");
return;
}
}

if(chk == true) {
var newDigit = digit.split(".");
var newDigit1 = newDigit[0];
var newDigit2 = newDigit.length > 1 ? '.' + newDigit[1] : '00';
var commas = /\,/g
var isDigit = /^[1-9]/;
var isDollor = /^\$/;
if(isDigit.test(newDigit1) || isDollor.test(newDigit1)) {
if(commas.test(newDigit1)) {
newDigit1 = replaceCommas(newDigit1.replace(/\,/g, ""));
newDigit1 = replaceCommas(newDigit1.replace(/\$/g, ""));
} else {
newDigit1 = replaceCommas(newDigit1);
newDigit1 = replaceCommas(newDigit1.replace(/\$/g, ""));
}

if (newDigit[1]) {
obj.value = "$" + newDigit1 + newDigit2;
} else {
obj.value = "$" + newDigit1 + "." + newDigit2;
}
} else {
alert("Not a valid number, All number cannot be Zero \nNumber format should be like this 1,000,000.00");
}
} else {
alert("Not a valid number \nNumber format should be like this 1,000,000.00");
}
}

function replaceCommas(digit) {
var regExp = /(\d+)(\d{3})/;
while(regExp.test(digit)) {
digit= digit.replace(regExp, '$1' + ',' + '$2');
}
return digit;
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Validating Number in Javascript  .  Number Validation with Digit Grouping in Javascript  .  Number Conversion in Javascript  .  Digit Grouping in Javascript  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Get the text of label using Javascript
Previous Resource: How to call a serverside method in ASP.NET using javascript
Return to Discussion Resource Index
Post New Resource
Category: Javascript


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use