Solved

Asp.Net JavaScript only Decimals

Posted on 2006-11-09
22
639 Views
Last Modified: 2010-05-18
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
Comment
Question by:RamyBouchra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • +1
22 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17905625
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17905776
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
 
LVL 9

Expert Comment

by:kraffay
ID: 17907812
I recommend getting a free masked text box control from here:

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

All server side, no coding.  
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

by:RamyBouchra
ID: 17925243
thank you for your reply
but i want to do it from JavaScript.
so please can anyone Help me.

thank you
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17925458
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17925460
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
 

Author Comment

by:RamyBouchra
ID: 17925603
dear sir "kraffay "
how to make the format i want to make as stated in my question by this custom control.
thank you
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17925641
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17925645
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
 

Author Comment

by:RamyBouchra
ID: 17930790
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17930913
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
 

Author Comment

by:RamyBouchra
ID: 17931102
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17931192
OK. What behaviour are you trying to achieve here? What do you want to happen if the value is invalid?
0
 

Author Comment

by:RamyBouchra
ID: 17931634
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17931905
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
 

Author Comment

by:RamyBouchra
ID: 17932488
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
 

Author Comment

by:RamyBouchra
ID: 17932492
sorry
now it take only 14 digits + decimal point only
0
 

Author Comment

by:RamyBouchra
ID: 17932566
onething more if you please
it can take 2 decimal point if they are not after each other

123.33.


thank you.
0
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 17932809
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
 

Author Comment

by:RamyBouchra
ID: 17932883
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
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17933274
It's a pleasure.
0
 

Author Comment

by:RamyBouchra
ID: 17936652
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question