• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

How to make the last two numbers typed into a asp:textbox decimals?

Hello Experts,

I have an asp:textbox with the following regular expression validator:

<asp:RegularExpressionValidator ID="rev_case_exp_cost_txt" runat="server" ControlToValidate="case_exp_cost_txt" Type="Integer" Operator="DataTypeCheck" ErrorMessage="Invalid Format" ValidationExpression="^\d{1,11}(?:\.\d\d)?$"></asp:RegularExpressionValidator>

Open in new window


How can I get it to automatically make the last two numbers entered as the decimals?  For example, if the user types 300, it should show 3.00 after they tab out of the field - or 50000 would be 500.00.  

I am using asp.net and c#.

Any help would be greatly appreciated.

Thanks,
J
0
ferguson_jerald
Asked:
ferguson_jerald
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Majority of the script taken from here:
http://bytes.com/topic/javascript/answers/556779-add-decimal-number

Splice function found online found online


add this to your textbox:

 onBlur="checkDecimal(this,this.value)

<script type="text/javascript">
function checkDecimal(obj, objStr){
    var objNumber;
    if(isNaN(objStr) && objStr!=''){
        alert('Value entered is not numeric');
        objNumber = '0.00';
    }
    else if(objStr==''){
        objNumber = '0.00';
    }
    else if(objStr.indexOf('.')!=-1){
        if(((objStr.length) - (objStr.indexOf('.')))>3){
            objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
        }
        if(objStr.indexOf('.')==0){
            objStr = '0' + objStr;
        }
        var sLen = objStr.length;
        var TChar = objStr.substr(sLen-3,3);
        if(TChar.indexOf('.')==0){
            objNumber = objStr;
        }
        else if(TChar.indexOf('.')==1){
            objNumber = objStr + '0';
        }
        else if(TChar.indexOf('.')==2){
            objNumber = objStr + '00';
        }
    }
    else{
        objNumber = objStr.splice(len(objstr)-2,0,'.');
    }
    obj.value = objNumber;
}

String.prototype.splice = function( idx, rem, s ) {
    return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
};
</script>

Open in new window

0
 
Manoj PatilSr. Software EngineerCommented:
Hi you can try JQuery for this.
Check this link
http://www.texotela.co.uk/code/jquery/numeric/
And Sample Code for this is attached below.
HTML CODE
SampleCode.html

JAVASCRIPT FILES
jquery.numeric.js

jquery.numeric.min.js
0
 
ferguson_jeraldAuthor Commented:
I tested both suggestions and they both worked!  Thanks for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now