Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Asp.Net JavaScript only Decimals

Posted on 2006-11-09
22
Medium Priority
?
649 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

598 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