I need to write use a custom validator to check to see if a textbox has a number with a decimal for instance 95.3 in it. What is the expression to accomplish in validator server controls in asp.net?
4 Answers
Not sure if this is exactly what you're looking for, but there's this post I've seen a long time ago which kinda explains how to do it. You might wanna take a look: http://aspdotnet-sequel.blogspot.com/2009/05/aspnet-textbox-validate-integer-float.html
Comments
In the validator_ServerValidate event you can do this:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
If args.Value.Contains(".") Then
'*** Textbox has a decimal...do what you need to handle it ***'
End If
End Sub
But as others mention, without knowing a bit more about what exactly you need to have happen, you would probably want to look at either a Regular Expression validator and try to validate on the client-side instead of server-side (unless you need to have it happen on the postback)
2 Comments
Something like this should work, you can modifiy to allow as many decimal places you need or want:
\d{0,}.\d{0,2}
Assuming you are speaking about regular expressions. But if you were you should use a regular expression validator instead of a custom one.
Also, here is a link to another question that is very similar. link