Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.18/5 (4 votes)
See more:
Hai All,


i have a text box in asp.net it will accept only number and decimals only in the format of XX,YY not more than two nnumbers means

ex:12,45
12.45,24.5
0.57,34.56

not like this error:

Ex:12.34.34,23.45
,23.346,34
then it will show error

plz any one help me very urgent
Posted
Comments
Thanks7872 20-Aug-14 9:56am    
34.56 is acceptable then why not 23.45?
[no name] 23-Aug-14 1:26am    
how it possible this question.u said that it will took only decimal number then y r u putting 12.34.34? its a decimal number ? please check it..!!!

Hello,
Basically You Can Use Javascript/Jquery for this for this.you should assign a specific class such as amount , amountlbl to each textbox & label having number.
The Number should be like this :-12345 to 12,345 not 1,23,45 means in multiple of 3's. But you can modify the regular expression/functions to your requirements[CommaSepratedValues].I have tired this code for single number in textbox or label.I have assisgned a CSS class amount to textbox and label as amountlbl.


Here is the Java Script Code Using Regular expression
JavaScript
function CommaSepratedValue(nStr)
 {    try{
    if(nStr!=undefined || nStr !=null){
     nStr = nStr.replace(/,/g,'');
     nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;//For 2 numbers replace d{3} to d{2}
    while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return x1 + x2;
     }
     }
  catch(er){
      return nStr;
      }
    
  }



Now give the reference to jquery library in head tag
In the end write
JavaScript
$(document).ready(function(){
 $(function(){
     
        $(".Amount").keyup(function(){
            this.value= CommaSepratedValue(this.value);
        });
       
      
     $(".Amount").each(function(){ 
            if(this.value!=undefined||this.value!=null)
                this.value=CommaSepratedValue(this.value);      
        });
        $(".Amountlbl").each(function(){
         this.innerHTML= CommaSepratedValue(this.innerHTML);
        });
         
    });
  });


Hope this Helps You.if you come across any error/difficult let me know.

Happpy Coding :-)
 
Share this answer
 
v3
Use a regular expression. I'd suggest a Regular Expression validator to process the error on the client side.

Writing regex expressions can be nasty and if you dont' want to do that yourself, use an online regex library/generator. I normally use http://www.regexlib.com/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900