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

Asp.Net JavaScript only Decimals

Dear Experts.

I am using ASP.NET2.0, C#.
I want to make validation on a textbox that only Accepts Money and dose not Exceeds 14 digits and 1 decimal point and 2 No. after the Decimal places, throught JavaScript.
0
RamyBouchra
Asked:
RamyBouchra
  • 10
  • 10
  • +1
1 Solution
 
Bob LearnedCommented:
Hmmm, that would be much easier with code in the code-behind, so it might be possible with AJAX to access a code-behind method from Javascript.

Bob
0
 
muzzy2003Commented:
Why don't you use an ASP.NET RegularExpressionValidator? For browsers that support JavaScript this will perform the validation in JavaScript, and you have a fallback if it doesn't. A suitable regular expression might be:

\d{1,11}\.\d{2}
0
 
kraffayCommented:
I recommend getting a free masked text box control from here:

http://www.eworldui.net/CustomControls/NumericBox.aspx

All server side, no coding.  
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RamyBouchraAuthor Commented:
thank you for your reply
but i want to do it from JavaScript.
so please can anyone Help me.

thank you
0
 
muzzy2003Commented:
function Validate(s) {
    var re = new RegExp("^\d{1,11}\.\d{2}$");
    return re.test(s);
}

Returns true if the string matches the required pattern false otherwise.
0
 
muzzy2003Commented:
But I'd still recommend the RegularExpressionValidator control - you get JavaScript regular expression validation without having to hand-craft the script, plus server-side validation as a backup when browsers don't support JavaScript or have it disabled.
0
 
RamyBouchraAuthor Commented:
dear sir "kraffay "
how to make the format i want to make as stated in my question by this custom control.
thank you
0
 
muzzy2003Commented:
Looks like this would do the number of decimal places:

<ew:NumericBox runat="server" id="nbDemo4" DecimalPlaces="2" />

Not sure about the maximum length though. If you are considering going this way, suggest you download the control and have a play.
0
 
muzzy2003Commented:
Sorry, realised there is a problem with the JavaScript I posted:

function Validate(s) {
    var re = new RegExp("^\\d{1,11}\\.\\d{2}$");
    return re.test(s);
}

This should work.
0
 
RamyBouchraAuthor Commented:
Dear Mr.muzzy2003
i want to ask you a question"
1 - what is the event that i should use.
2 - what should i pass to this param "S".
3 - what is "test".

thank you So much for your reply
0
 
muzzy2003Commented:
This function will return a boolean indicating whether a string you pass to it is valid. You will need to write an event handler that passes the text value in the input box to this function, and reacts accordingly. I can't answer the question about which event you need without knowing how you are currently validating things on your page. "test" is a method on the JavaScript RegExp object.
0
 
RamyBouchraAuthor Commented:
Dear Mr.Muzzy2003

thank you for your quick reply.

this is the code i wrote
this.TextBox1.Attributes.Add("onkeypress", "Validate()");or "onkeyup"

function Validate()
{
    var s = document.getElementById('<%=TextBox1.ClientID%>').value;
   
    var re = new RegExp("^\\d{1,11}\\.\\d{2}$");
    return re.test(s);
}

please tell me what is wrong, it is not working.

thank you
0
 
muzzy2003Commented:
OK. What behaviour are you trying to achieve here? What do you want to happen if the value is invalid?
0
 
RamyBouchraAuthor Commented:
thank you for your quick response
i dont want anything to happned just i want to if the press any char. like a,d anything that is not number
then it is not written at all, in the same time the user have the ability to write only numbers and 1 decimal point
and i dont want the no. of digits befor the decimal point to exceed 14 digit + 2 numbers after the decimal point

i hope you understand me now.

thank you so much.
please if you have any more question ask me.

0
 
muzzy2003Commented:
OK. What you are trying to achieve is not exactly validation, then, but masking. I'd try trapping the onkeypress event, and sending it to this method:

function MaskMoney(evt) {
    if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57))) return false;
    var parts = evt.srcElement.value.split('.');
    if (parts.length > 2) return false;
    if (parts.length == 1 && evt.keyCode == 46) return true;
    if (parts[0].length >= 14) return false;
    if (parts.length == 2 && parts[1].length >= 2) return false;
}

You woul then set your onkeypress event to "return MaskMoney(event);".

You might need to tweak the script a bit.
0
 
RamyBouchraAuthor Commented:
thank you so much but small change if you please
i want it to take 14 digits + 2 digits after the decimal point.

thank you
0
 
RamyBouchraAuthor Commented:
sorry
now it take only 14 digits + decimal point only
0
 
RamyBouchraAuthor Commented:
onething more if you please
it can take 2 decimal point if they are not after each other

123.33.


thank you.
0
 
muzzy2003Commented:
Is this any better?

function MaskMoney(evt) {
    if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57))) return false;
    var parts = evt.srcElement.value.split('.');
    if (parts.length > 2) return false;
    if (evt.keyCode == 46) return (parts.length == 1);
    if (parts[0].length >= 14) return false;
    if (parts.length == 2 && parts[1].length >= 2) return false;
}

It's hard to get what you want in a perfect way - that would take some quite complicated JavaScript.
0
 
RamyBouchraAuthor Commented:
thank you
so much God Bless you
this is my mail <email deleted>
it is a pleasure for me if you added me if you are using MSN.

other thing please
if you have any project i we can do it for you we 15$ per/h
we are in egypt.
we already have client in USA.

thank you.

Email deleted by Page Editor
0
 
muzzy2003Commented:
It's a pleasure.
0
 
RamyBouchraAuthor Commented:
Dear Mr.Muzzy

i have a lot of questions
i want to ask you.
so how can you help me
i already post them in this site but i didnt see your reply
can you give me your mail.

thank you.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 10
  • 10
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now