Solved

Asp.Net JavaScript only Decimals

Posted on 2006-11-09
22
621 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now