RamyBouchra
asked on
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.
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.
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}
\d{1,11}\.\d{2}
I recommend getting a free masked text box control from here:
http://www.eworldui.net/CustomControls/NumericBox.aspx
All server side, no coding.
http://www.eworldui.net/CustomControls/NumericBox.aspx
All server side, no coding.
ASKER
thank you for your reply
but i want to do it from JavaScript.
so please can anyone Help me.
thank you
but i want to do it from JavaScript.
so please can anyone Help me.
thank you
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.
var re = new RegExp("^\d{1,11}\.\d{2}$"
return re.test(s);
}
Returns true if the string matches the required pattern false otherwise.
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.
ASKER
dear sir "kraffay "
how to make the format i want to make as stated in my question by this custom control.
thank you
how to make the format i want to make as stated in my question by this custom control.
thank you
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.
<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.
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.
function Validate(s) {
var re = new RegExp("^\\d{1,11}\\.\\d{2
return re.test(s);
}
This should work.
ASKER
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
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
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.
ASKER
Dear Mr.Muzzy2003
thank you for your quick reply.
this is the code i wrote
this.TextBox1.Attributes.A dd("onkeyp ress", "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
thank you for your quick reply.
this is the code i wrote
this.TextBox1.Attributes.A
function Validate()
{
var s = document.getElementById('<
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
OK. What behaviour are you trying to achieve here? What do you want to happen if the value is invalid?
ASKER
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.
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.
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.
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.
ASKER
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
i want it to take 14 digits + 2 digits after the decimal point.
thank you
ASKER
sorry
now it take only 14 digits + decimal point only
now it take only 14 digits + decimal point only
ASKER
onething more if you please
it can take 2 decimal point if they are not after each other
123.33.
thank you.
it can take 2 decimal point if they are not after each other
123.33.
thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
It's a pleasure.
ASKER
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.
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.
Bob