CustomValidator for Decimal Number using JavaScript in ASP.Net

Last Reply 3 months ago By dharmendr

Posted 3 months ago

i am using this regular expression to validate decimal point it works fine


but i want to add one more logic in it

if user type 0.30 it says invalid i want it to be valid but if user type 00.3 this should be invalid i guess in below expression if make it this so this will be good 0.30 but i want to add one more logic that if usre type 00.30 means in start 2 zero is invalid how to do that

<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtofferrate" CssClass="validation" Display="Dynamic" ErrorMessage="Valid Decimal Point Required" ValidationExpression="^[1-9]\d*(\.\d+)?$" ></asp:RegularExpressionValidator>


You are viewing reply posted by: dharmendr 3 months ago.
Posted 3 months ago

Hi nauna,

With RegularExpressionValidator it is not possible. For this you need to use CustomValidator and write your own logic.

Check this example. Now please take its reference and correct your code.


<asp:TextBox runat="server" ID="txtofferrate" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="Validate"
    CssClass="validation" Display="Dynamic" ControlToValidate="txtofferrate" ErrorMessage="" ForeColor="Red" />


<script type="text/javascript">
    function Validate(sender, args) {
        var offerRate = document.getElementById(sender.controltovalidate).value;
        var regex = /^[0-9]\d*(\.\d+)?$/;
        if (regex.test(offerRate)) {
            if (offerRate.split('.').length > 1) {
                var value = offerRate.split('.')[0];
                if (parseInt(value) == 0) {
                    if (value.length > 1) {
                        sender.innerHTML = "Valid Decimal Point Required."
                        args.IsValid = false;
            else {
                args.IsValid = true;
        } else {
            sender.innerHTML = "Valid Decimal Point Required."
            args.IsValid = false;