Click here to Skip to main content
15,907,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,


In my web application i have some fields that are Lenght, Breadth,Height, Distance.
Here my requirement is while get input date for this fields from user i need to check how many digits are entered after decimal point value, for example 233.345.

if user enter value like 3443.83839 it should through error msg to user that after decimal point only 3 digits should be enter.


How can i achive this with customvalidator, please give me the coding and suggest me to get it.

Thanks all

uday
Posted

Since the textbox only returns a string, use the IndexOf method to give you the position of the decimal point, and compare it against the Length of the Text property.
 
Share this answer
 
Comments
udaysimha 22-Nov-11 4:59am    
Hi

Thanks for your reply.Please can you explain with some example code.

Any help would be appriciated.

Thanks
Uday
OriginalGriff 22-Nov-11 5:05am    
You are joking, right?
string s = "123.123";
int decPos = s.IndexOf('.') + 1;
if (decPos >= 1 && (s.Length - decPos) > 3)
{
// Bad user!
Console.WriteLine("Bad");
}
udaysimha 22-Nov-11 5:30am    
Hi Boss,

Thanks for your quick reply, i am not joking, i am a beginer past 2 monts only i am working in asp.net.

Actual my requirement is i am tryint to validate textbox value with CustomeValidator to check how many digits are entered after decimal point if it exits.If it is more then 3 digits it should not be accept user input.

just follow my code what i tryed
function Chkdecimalvalidation(serber, args)
{
var txtval=args .Value;
var splitvalue=txtval.Value.split(".",2);

if(isNaN (splitvalue[0])|| isNaN (splitvalue[1]))
{
if(splitvalue[1]!= null )
{
if(splitvalue [1].length == 3)
{
args .IsValid=true ;
}
else
{
args .IsValid=false ;
}
}
else
{
args .IsValid=false ;
}
}
else
{
args .IsValid=false ;
}
}
i am not getting correct result. I think split() is not avilable here.

Please help me.

Thanks
OriginalGriff 22-Nov-11 5:37am    
Right, so you are doing it in Javascript.
No, you don't want or need Split.
Look at my example, and at these two links:
http://www.w3schools.com/jsref/jsref_indexof.asp
http://www.w3schools.com/jsref/jsref_length_string.asp
udaysimha 22-Nov-11 6:04am    
HiI looked at your exampls and links, in that founds only index of a specified character.
Here my requirement is to find decimal point if it exits and no.of digits after decimal.If there is no decimal point no need to check the user input.

I am calling Chkdecimalvalidation(serber, args) function from CustomValidator here.
Hi udaysimha,

try using reg-ex for it.

Try this:

^[0-9]([\.\,][0-9]{1,3})?$

Hope it helps :)

Regards,
Eduard
 
Share this answer
 
v2
Comments
udaysimha 22-Nov-11 5:38am    
Hi Eduard Lu

Thanks for your reply,i tryed with you suggestd reg-ex ^[0-9]([\.\,][0-9]{1,3})?$

it`s not working, i tryed by givig values like 234(should accept),
233.334(should accept), 23.1(shold not accept), 233.43235(should not accept)

follow my code:

<asp:RegularExpressionValidator ID="revlength" runat ="server" ControlToValidate ="txtLength"
ErrorMessage="wrong" ValidationExpression ="^[0-9]([\.\,][0-9]{1,3})?$" >

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900